本文给大家介绍关于include文件赋值的问题,希望对需要的朋友有所帮助!
PHP实战教程://m.sbmmt.com/k.html
具体问题:
include文件:
<?php $_CFG = array(); $_CFG['DB_HOST'] = 'localhost'; $_CFG['DB_USER'] = 'root'; $_CFG['DB_PASWORD'] = 'liujianian'; $_CFG['DB_NAME'] = 'commerceClub';
<?php class conf{ protected static $ins = null; protected $data = array(); final protected function _construct(){ //一次性将配置文件读取进来 include('./config.php'); $this->data = $_CFG; } final protected function _clone(){ } public static function getIns(){ if(self::$ins instanceof self){ return self::$ins; } else{ self::$ins = new self(); return self::$ins; } }} $conf = conf::getIns();print_r($conf);
测试结果是:
conf Object ( [data:protected] => Array ( ) )
为什么数组$_CFG中的值没有被赋给全局变量protected $data呢?
解决方案:
因为你漏掉了一个下划线
__construct __clone