php包含课堂上的文件

Jer*_*dev 13 php variables class include

是否可以在类中包含带有php变量的文件?那么如何才能获得全班级内的数据?

我已经谷歌搜索了一段时间,但没有一个例子有效.

谢谢你,杰罗德夫

Mih*_*rga 14

最好的方法是加载它们,而不是通过外部文件包含它们

例如:

// config.php
$variableSet = array();
$variableSet['setting'] = 'value';
$variableSet['setting2'] = 'value2';

// Load config.php ...
include('config.php');
$myClass = new PHPClass($variableSet);

// In a class you can make a constructor
function __construct($variables){ // <- As this is autoloading, see http://php.net/__construct
    $this->vars = $variables;
}
// And you can access them in the class via $this->vars array
Run Code Online (Sandbox Code Playgroud)