它有可能成为一个静态变量接收器“;需要__DIR__'/paths.php'&”;


Its possible to a static var recieve "require __DIR__.'/paths.php'"?

工作区:php 5.4,apache 2.4

我的代码:

class Test{
    private static $paths = array();
    self::$paths = require __DIR__.'paths.php';
    //another code
}

可以用这个吗?我的目标是创建一个变量来接收一个数组,该数组包含应用程序的许多路径。

注意,那个require或多或少是include的别名。这两个声明都加载并执行文件这些函数不返回任何内容

或多或少,你总是想使用这些更严格的声明:

  • require_one()
  • include_one()

我建议采用一种纯粹的OO设计:

require( __DIR__  . '/CClassProvider.php' );
class CTestCase {
  protected $_pathArray;
  function __construct() {
      $this->_pathArray = CClassProvider::pathArray();  // from paths.php
   }
}

CClassProvider.php

class CClassProvider {
  static function pathArray() {
    array( '/usr/2', '/usr/3' );
  }
}