Array in config file php

Saved searches

Use saved searches to filter your results more quickly

You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session. You switched accounts on another tab or window. Reload to refresh your session.

Update php array values programmatically. This library allows you update specific php array elements in file.

hollax/ArrayConfigWriter

This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?

Sign In Required

Please sign in to use Codespaces.

Launching GitHub Desktop

If nothing happens, download GitHub Desktop and try again.

Читайте также:  Вызов хранимой процедуры java

Launching GitHub Desktop

If nothing happens, download GitHub Desktop and try again.

Launching Xcode

If nothing happens, download Xcode and try again.

Launching Visual Studio Code

Your codespace will open once ready.

There was a problem preparing your codespace, please try again.

Latest commit

Git stats

Files

Failed to load latest commit information.

README.md

This php library can be used to update array values in a php file. The library can be used by applications that use php array to store configuration values. It makes updating config array possible programatically.

  • Download the library and extract it to a folder in your application. The folder choice depends on your application structure.

The class supports autoload via composer

$config_writer = new Array_Config_Writer($config_file, $variable_name , $auto_save );
  • $config_file (string) : The absolute path to the file where the array is declared.
  • $variable_name (string) : The variable name of the array to update.
  • $auto_save (boolean) : Whether the library should automatically save the changes.

We can start updating values:

$config_writer->write('key' , value ); 
  • You can set value to any php variable type.
  • The library treats numeric index «as is». Meaning ’21’ is different from 21

Supported variable Styles:

note You can not use the library to update the following format:

  • The library expect the variable to be indexed.
  • The file can have other variables aside our target variable.

PHP File config.php

/* |-------------------------------------------------------------------------- | Site Name |-------------------------------------------------------------------------- | | | */ $config[ 'site_name'] = 'Example Site'; /* |-------------------------------------------------------------------------- | Enable caching |-------------------==------------------------------------------------------- | | */ $config[ 'enable_caching'] = true; /* |-------------------------------------------------------------------------- | Custom Array |-------------------==------------------------------------------------------- | | */ $config[ 'message'] = array( 'title' => 'Welcome' , 'body' => 'Thanks for your interest in the library' ); /* |-------------------------------------------------------------------------- | Another Config Variable for the database |-------------------==------------------------------------------------------- | | */ $db[ 'database'] = ''; $db[ 'username'] = '';

Create an instance of the library:

$config_writer = new Array_Config_Writer( APP_PATH.'config/config.php', 'config' );

Update a value by index. The site_name for instance:

$config_writer->write('site_name' , "New Site Name' );

The file config.php should be updated

$config_writer->write('site_name' , "New Site Name' ) ->write('enable_caching' , false );

To update the ‘message’ index which has array has value

$message['title'] = 'My New title' ; $message['body'] = 'New message body' ;
  • Or completely set new value for the message index (assuming the admin posted form. Ideally you would validate submission)

You need phpunit to run the test cases

About

Update php array values programmatically. This library allows you update specific php array elements in file.

Источник

Create Config Files in PHP

Create Config Files in PHP

  1. Store the Configurations in an Array in a PHP File
  2. Typecast the Array Configuration to an Object in a PHP File
  3. Use an INI File to Create a Config File in PHP

This article will introduce some methods to create config files in PHP.

Store the Configurations in an Array in a PHP File

We can create an array of the configuration items to form a config file in PHP.

An associative array will be best for storing the configuration data. The file can be included in another PHP file, and the configuration can be used.

We can use the include() function to have the configuration file. We will demonstrate the creation of config files for the database connection in PHP.

For example, create a PHP file config.php and create an array. Write the keys hostname , username , and password in the array.

Set the hostname to localhost , username , and password to your database username and password. Then return the array.

Next, create another file, DB.php , and use the include() function to include the config.php file and store it in the $conf variable. Create the variables $hostname , $username , $password , and $db to assign the configurations.

Store the configurations in the variables accessing each array items of the $conf variable. Set your database name in the $db variable. Use the mysqli_connect() function to connect the server and the database.

Provide the variables we created above as the parameters to the mysqli_connect() function. If the correct configuration is provided in the config.php file, the example below will connect the server and the database.

In this way, we can use an array to create a configuration file in PHP.

return array(  'hostname' => 'localhost',  'username' => 'root',  'password' => 'pass123' ); 
$conf = include('config.php');  $hostname = $conf['hostname']; $username = $conf['username']; $password = $conf['password']; $db = "my_db";  $con = mysqli_connect($hostname, $username, $password,$db);  if (!$con)  die("Failed to establish connection"); > echo "Connection established successfully"; 
Connection established successfully 

Typecast the Array Configuration to an Object in a PHP File

This method will typecast the array in the config.php file into an object. In this way, the configurations can be accessed as an object in the PHP file.

Furthermore, we can take the benefits from objects for data handling purposes. For example, the objects could easily be passed JSON to the client-side if we use JavaScript.

For example, typecast the array placing (object) before the array function after the return keyword. Then, in index.php , access the configurations as $conf->hostname as shown in the example below.

return (object) array (  'hostname' => 'localhost',  'username' => 'root',  'password' => 'subodh',  'db' => 'oop' ); 
$conf = include('config.php');  $hostname = $conf->hostname; $username = $conf->username; $password = $conf->password; $db = $conf->db;  $con = mysqli_connect($hostname, $username, $password,$db);  if (!$con)  die("Failed to establish connection"); > echo "Connection established successfully"; 

Use an INI File to Create a Config File in PHP

We can also create a config file using the INI file in PHP. We can store all the configurations in the INI file and use the parse_ini_file() function to access those configurations in the PHP file.

The INI file is broadly used as a configuration file. The structure of the file contains key-value pairs.

The parse_ini_file loads the content of the INI file as an associative array. We will demonstrate the creation of the INI config file to establish a connection to the server and the database.

For example, create a file config.ini and write the keys hostname , username , password , and db . Then, write the correct configurations. An example is shown below.

hostname = 'localhost' username = 'root' password = 'pass1234' db = 'my_db' 

Next, create an index.php file and create an $ini variable in it.

Use the parse_ini_file() function to parse the config.ini file. The $ini variable contains an associative array of the configurations.

Now use the mysqli_connect() function with the correct parameters to establish the connection as we did above.

$ini = parse_ini_file('config.ini');  $hostname = $ini['hostname']; $username = $ini['username']; $password = $ini['password']; $db = $ini['db'];  $con = mysqli_connect($hostname, $username, $password,$db);  if (!$con)  die("Failed to establish connection"); > echo "Connection established successfully"; 
Connection established successfully 

It should be noted that the INI file should be kept in a non-public folder so that we do not encounter any security problems. In this way, we can create a configuration file with an INI file.

Subodh is a proactive software engineer, specialized in fintech industry and a writer who loves to express his software development learnings and set of skills through blogs and articles.

Источник

Saved searches

Use saved searches to filter your results more quickly

You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session. You switched accounts on another tab or window. Reload to refresh your session.

Allows for using native PHP files as config files in PHP!

mrkrstphr/array-config

This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?

Sign In Required

Please sign in to use Codespaces.

Launching GitHub Desktop

If nothing happens, download GitHub Desktop and try again.

Launching GitHub Desktop

If nothing happens, download GitHub Desktop and try again.

Launching Xcode

If nothing happens, download Xcode and try again.

Launching Visual Studio Code

Your codespace will open once ready.

There was a problem preparing your codespace, please try again.

Latest commit

Git stats

Files

Failed to load latest commit information.

README.md

Allows for using native PHP files as config files in PHP!

Using YAML and XML as configuration scripts in PHP projects has become prolific. However, this comes with the overhead of having to validate, process, and convert those configuration files to PHP to be used.

What if you could just use native PHP arrays for configuration files?

Now you can!

periwinkle/array-config brings revolutionary support to PHP to use PHP in PHP.

periwinkle/array-config can be installed via Composer:

composer require periwinkle/array-config 

Let’s say you had a configuration file written in Yaml:

database: driver: pgsql host: localhost name: awesomedb user: awesomesauce password: mustangSally

With periwinkle/array-config, you can now write this configuration file as pure PHP!

return [ 'database' => [ 'driver' => 'pgsql', 'host' => 'localhost', 'name' => 'awesomedb', 'user' => 'awesomesauce', 'password' => 'mustangSally' ] ];

To use this config file, simply:

$config = include 'configs/database.php';

No need to parse Yaml! No need to parse Xml!

array-config is so revolutionary, we’ve added support going all the way back to PHP 3! The examples above use the short array syntax introduced in PHP 5.4. If you’re living in the past, you can still use array-config*:

return array( 'database' => array( 'driver' => 'pgsql', 'host' => 'localhost', 'name' => 'awesomedb', 'user' => 'awesomesauce', 'password' => 'mustangSally' ) );
$config = include 'configs/database.php';

* Note: Composer will not work with some older versions. For those versions, feel free to copy the array-config source code to a directory within your project. Any directory will do; we suggest a functions/ or includes/ directory if you have one.

Ping your favorite neighborhood framework developer, and tell them you want the ability to use native PHP config files in their projects!

Yeah, I know you can just cache the result of the Yaml or Xml parsing so that it doesn’t have to be done over and over again, but there’s no valid reason to use it in the first place.

Just use PHP.

About

Allows for using native PHP files as config files in PHP!

Источник

Оцените статью