How to include a variable inside a class in php
class test1 { function test2 { global $config_key_security; echo $config_key_security; } } or
class test1 { function test2 { echo $GLOBALS['config_key_security']; } } If your class relies on a global variable, this is actually not the best practice - you should consider passing it to the constructor instead.
Create your configuration file an array of configuration items. Then include this file in your class constructor and save its value as a member variable. Thus, all your configuration settings are available for the class.
test.php:
<? $config["config_key_security"] = "test"; $config["other_config_key"] = true; ... ?> test5.php:
<? class test1 { private $config; function __construct() { include("test.php"); $this->config = $config; } public function test2{ echo $this->config["config_key_security"]; } } ?> You can use the $ GLOBALS array of variables and put your global variable in it.
For example: File: configs.php
<?PHP $GLOBALS['config_key_security'] => "test"; ?> File: MyClass.php
<?php require_once 'configs.php'; class MyClass { function test() { echo $GLOBALS['config_key_security']; } }