Php 包含的文件是否包含它包含的文件?
我有一个源文件夹/config.php文件:Php 包含的文件是否包含它包含的文件?,php,class,object,include,Php,Class,Object,Include,我有一个源文件夹/config.php文件: <?php $config['database'] = array ( 'host' => 'localhost', 'user' => 'root', 'pass' => '', 'db' => 'game' ); ?> <?php include_once $_SERVER['DOCUMENT_ROOT'].'config.php'; function __autoload($s
<?php
$config['database'] = array (
'host' => 'localhost',
'user' => 'root',
'pass' => '',
'db' => 'game'
);
?>
<?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 {
}
<?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];
}
}
?>
在core.class.php文件和
static function GetConfigArray($name) {
$config = Core::getWholeConfig();
return $config[$name];
}
在config.class.php文件中,但我仍然不明白为什么最后一个文件没有看到
$config
变量。我的意思是不在类的范围内,但在任何地方,这个变量都包含在core.class.php中,尽管core.class.php包含在config.class.php中,但变量本身并没有。为什么?将配置作为返回变量放置到函数中
function getConfig(){
$config['database'] = array (
'host' => 'localhost',
'user' => 'root',
'pass' => '',
'db' => 'game'
);
return $config;
}
然后在课堂上,你可以使用:
$config = getConfig();
return $config[$name];
我把
var\u转储($config)代码>位于的顶部config.class和core.class。如果我只是在浏览器中加载config.class,那么
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)
仔细阅读函数中变量的可用性,我知道我不能返回$config[$name]
,因为变量在类和方法的作用域之外声明。但是,为什么我试图将变量转储到类外部的同一个文件中,该文件将其转储为NULL?实际全局范围中是否存在$config数组取决于从何处调用include脚本。默认情况下,包含的脚本不会在全局范围内运行。自动加载程序有一个本地函数作用域。我不想在config.php文件中包含任何函数,但我解决了它,谢谢。不管怎样,我的问题是其他的,你能查一下我上次的编辑吗?我用我的结果更新了我的帖子。只要它不在函数或类中,它在包含它的任何文件中都是可见的。无论如何,谢谢你的帮助,我在这方面每天都在进步:)
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)