hi, when i open a file with name config.php which its content is array like this <?php $CONFIG['ddd'] = 'hello'; ?> PHP: and another file withe name test.php which its content is <?php class fff { function fff() { require_once ("./config.php"); $this->ddd(); } function ddd() { print_r($CONFIG); } } $load = new fff; ?> PHP: it doesn't work and this also <?php class fff { function fff() { require_once ("./config.php"); $this->ddd(); } function ddd() { require_once ("./config.php"); print_r($CONFIG); } } $load = new fff; ?> PHP: but this work fine <?php class fff { function fff() { require_once ("./config.php"); $this->ddd(); } function ddd() { require ("./config.php"); print_r($CONFIG); } } $load = new fff; ?> PHP: i don't understand why ?!!
OK: Your first example won't work because $CONFIG isn't set to global, or to a class variable. So the next function does not know it was set before. The second one won't work because it won't include the config file again (hence ONCE.) So only the first function will be aware of the $CONFIG, and the second one won't have it included. The third one works because its including the $CONFIG array at the required time, and as many times as required. Definitely not a nice solution however. Dan
This should also work <?php class fff{ function fff() { $this->ddd(); } function ddd() { require_once ("./config.php"); print_r($CONFIG); } } $load = new fff;?>
i thins this one is the best <?php class fff { function fff() { global $CONFIG; require_once ("./config.php"); $this->ddd(); } function ddd() { global $CONFIG; print_r($CONFIG); } } $load = new fff; ?> PHP: thanks for all
just know that it should work.. however, manipulating globals within your classes is a really bad thing, and can lead to worse things..
Globals aren't fun in OOP. Here's an alternative example. <?php /** * Danltn | http://danltn.com/ * MSN: msn@danltn.com * Email: daniel@neville.tk * Started: 5/7/2008 17:12 (UK) * Tested: No * PHP 4/5: 5 * No warranty is given to code used */ class Test { private $config; public function __construct( &$config = array() ) { $this->config = &$config; } public function access() { print_r( $this->config ); } public function add($array = array()) { $this->config = array_merge($this->config, $array); } } $config = array( 'hello' => 'world' ); $t = new Test( $config ); $t->access(); /* Return: Array ( [hello] => world ) */ $config = array_merge( $config, array('a' => 'test', 'b' => 'blah') ); /* Add some extra stuff in the global scope. */ $t->access(); /* Return: Array ( [hello] => world [a] => test [b] => blah ) */ $t->add(array('c' => 'see!')); /* Add some stuff in the class scope */ $t->access(); /* Return: Array ( [hello] => world [a] => test [b] => blah [c] => see! ) */ PHP:
class fff{ function fff() { require_once ("./config.php"); $this->ddd(); } function ddd() { require_once ("./config.php"); print_r($CONFIG); } } $load = new fff; PHP: You have to understand these things. First you have to understand the different between require() and require_once() require() will include and evaluate a specific file. require_once() will include and evaluate a specific file during the execution of the script, but it will check the include file to see if it's aready include. If the include file is already included then it will not included again. This function already include the config.php file. function fff() { require_once ("./config.php"); $this->ddd(); } PHP: The function ddd() use the require_once to include the config.php file again but, config.php file is already include in function fff() so, it will not include the config.php file again. Alright, ddd() function try to print_r the $CONFIG variable but the $CONFIG variable is not a public variable and only include inside the fff() function, that's why it's not working. function ddd() { require_once ("./config.php"); print_r($CONFIG); } PHP: Take a look at the code below. This is how we can verify the config.php file is only include in fff() function. class fff{ private $config; function fff() { require_once ("./config.php"); $this->config = $CONFIG; $this->ddd(); } function ddd() { require_once ("./config.php"); //<------Ooops config.php file is already included in fff() function. No more include print_r($CONFIG); //<-----What the hel, i could not find $CONFIG variable, are you crazy? :) print_r($this->config); //<-----That's better, i can do that. } } $load = new fff; PHP: I recommend this code for you to use. require_once("./config.php"); class fff{ function fff() { $this->ddd(); } function ddd() { global $CONFIG; print_r($CONFIG); } } $load = new fff; PHP: hope it's help