简体   繁体   中英

does included file contain files included by it?

I've got this source_folder/config.php file:

<?php


$config['database'] = array (
  'host' => 'localhost',
  'user' => 'root',
  'pass' => '',
  'db' => 'game'
);

?>

This source_folder/class/core.class.php file:

<?php
include_once $_SERVER['DOCUMENT_ROOT'].'config.php';

function __autoload($sName) {
    $aName = explode('_',$sName);
    if($aName[0] == 'Model')
        include_once $_SERVER['DOCUMENT_ROOT'].'class/model/' . strtolower($aName[1]) . '.class.php';
    elseif($aName[0] == 'View')
        include_once $_SERVER['DOCUMENT_ROOT'].'class/view/' . strtolower($aName[1]) . '.class.php';
    elseif($aName[0] == 'Controller')
        include_once $_SERVER['DOCUMENT_ROOT'].'class/controller/' . strtolower($aName[1]) . '.class.php';
    elseif($aName[0] == 'Core')
        include_once $_SERVER['DOCUMENT_ROOT'].'class/' . strtolower($aName[1]) . '.class.php';
}

class Core {

}

And this source_folder/class/config.class.php file:

<?php

include_once $_SERVER['DOCUMENT_ROOT'].'class/core.class.php';

/**
 * Description of config
 *
 * @author Lysy
 */
class Core_Config extends Core {

    static function GetConfigArray($name) {
        return $config[$name];
    }
}

?>

When I put var_dump($config['database']); into core.class.php the result is dump of the variable. But when I put var_dump(Core_Config::GetConfigArray('database')); anywhere it dumps as NULL . Where is the problem? Is config.php included in core.class.php also included in config.class.php as it includes core.class.php ? From what I know it should be, but it appears that it don't.
EDIT: I also tried to put var_dump($config['database']); into config.class.php but it also dumps as NULL .

EDIT 2: I solved it using

class Core {

    static public function getWholeConfig() {
        global $config;
        return $config;
    }

}

in core.class.php file and

static function GetConfigArray($name) {
    $config = Core::getWholeConfig();
    return $config[$name];
}

in config.class.php file but still I don't understand why the last file doesn't see the $config variable. I mean not in the class scope, but anywhere, this variable is included in core.class.php and though core.class.php is included in config.class.php the variable itself is not. Why?

Place config as a return variable to a function as such

function getConfig(){
   $config['database'] = array (
     'host' => 'localhost',
     'user' => 'root',
     'pass' => '',
     'db' => 'game'
   );
   return $config;
}

Then in your class you can use:

   $config = getConfig();
   return $config[$name];

I put var_dump($config); at the top of both config.class and core.class. If I simply load config.class in my browser, I get

array (size=1)
  'database' => 
    array (size=4)
      'host' => string 'localhost' (length=9)
      'user' => string 'root' (length=4)
      'pass' => string '' (length=0)
      'db' => string 'game' (length=4)

array (size=1)
  'database' => 
    array (size=4)
      'host' => string 'localhost' (length=9)
      'user' => string 'root' (length=4)
      'pass' => string '' (length=0)
      'db' => string 'game' (length=4)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM