require_once不工作但需要在php中

时间:2014-03-19 15:57:44

标签: php variables

我需要从公共类中的方法内部访问配置变量。 存储变量的文件包含在ajax.php页面中,我也调用了类构造函数和他的方法

<?php
    include_once('../config/config.php');
    [...]
    $myclass = new myclass();
    $res = $myclass->mymethod();
    echo json_encode($res);
?>

我试图从myclass方法mymethod中打印存储在config.php中的$ config变量,但它没有做任何事情。 如果我尝试调用include_once(&#39; ../ config / config.php&#39;)它不会加载任何内容而$ config varialbe总是为空,而如果我尝试包含(&#39; ; ../ config / config.php&#39;)我可以毫无问题地访问$ config变量数据。

因此,如果我正确理解include和include_once是如何工作的,_once只是跳过导入文件&因为之前已经包含它。我的问题是,如何在不被强制包含(不是_once)的情况下访问$ config?

2 个答案:

答案 0 :(得分:2)

您的问题是理解变量范围。您需要一种方法来使您的配置数组可以访问类级方法。

最简单的方法是将副本传递给构造函数中的类(如果仅在一个方法中需要,则为单个方法):

//config.php

$config=array(
    'something'=>'value',
    'somethingelse'=>10
);

//myclass.php    
class MyClass{
    private $config;

    public function __construct(array $config){

        $this->config = $config;
    }

    public function mymethod(){

        return $this->config['something'];
    }
}

//index.php 
include_once('../config/config.php');

$myclass = new MyClass($config);

echo $myclass->mymethod();//outputs 'value'

如果这只是纯粹的readonly,另一个选择是在静态类方法中包装配置数组:

//config.php
class Config{
    public static function getConfig(){
    return array(
        'something'=>'value',
        'somethingelse'=>10
        );
    }
}

//index.php 
include_once('../config/config.php');

//anywhere in your code
$config = Config::getConfig();

echo $config['something'];

这会在每次调用时初始化一个新数组,因此它不是一个很好的选择。

第三个选项是将你的配置包装在一个单独的类中,该类只初始化一次但可以通过任何地方的静态方法调用获得

//config.php
class Config{
    public $config;
    private static $inst=null;

    private function __construct(){
        $this->config=array(
            'something'=>'value',
            'somethingelse'=>10
        );
    }

    public static function Instance(){
        if(static::$inst==null){
            static::$inst=new Config();
        }
        return static::$inst;
    }
}

//index.php 
include_once('../config/config.php');

//anywhere in your code

echo Config::Instance()->config['something'];

单身人士模式很方便,但却有(通常有争议很好的)问题。

Ultimatly第一个选项可能就是你所需要的。

答案 1 :(得分:1)

作为另一种方式做同样的事情:

的config.php

<?php 
return array(
    'hostname' => 'localhost',
    'username' => 'dev',
    'passwrod' => '',
);

MyConfig.php

<?php
class MyConfig extends ArrayObject
{
    public function __construct($filename)
    {
        parent::__construct();
        $this->setFlags(ArrayObject::ARRAY_AS_PROPS);

        if (!is_readable($filename)) {
            throw new InvalidArgumentException("Invalid file name: ".$filename);
        }
        $loaded_data = include($filename);

        foreach ($loaded_data as $index => $value) {
            $this->offsetSet($index,$value);
        }
    }
}

然后你就可以这样使用了 MyClass.php

class MyClass{
    private $config;

    public function __construct($config){
        $this->config = $config;
    }

    public function mymethod(){
        return $this->config->hostname .":". $this->config->username;
        // or even
        //return $this->config['hostname'] .":". $this->config['username'];
    }
}

MyConfig继承ArrayObject的好处是可以将数组值用作属性。