What are some potential pitfalls of using global arrays for configuration in PHP?
Using global arrays for configuration in PHP can lead to potential issues such as lack of encapsulation, making it difficult to track changes and manage dependencies. To avoid these pitfalls, it is recommended to use a more structured approach like using a configuration class or a configuration file.
class Config {
public static $config = [
'database' => [
'host' => 'localhost',
'username' => 'root',
'password' => 'password',
'database' => 'my_database'
],
'app' => [
'name' => 'MyApp',
'debug' => true
]
];
}
// Access configuration values
echo Config::$config['database']['host']; // Output: localhost
echo Config::$config['app']['name']; // Output: MyApp
Keywords
Related Questions
- How can a user input string be passed to a stored procedure in PHP?
- How can temporary files be securely created and managed for downloading decrypted files in PHP without compromising security?
- What are some alternative methods to cURL for fetching external data in PHP, considering cURL may not be available everywhere?