Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/289.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在PHP中访问私有变量中的公共变量_Php_Oop - Fatal编程技术网

如何在PHP中访问私有变量中的公共变量

如何在PHP中访问私有变量中的公共变量,php,oop,Php,Oop,我有以下PHP类: class myClass extends AJAX_Callable { public static $classGlobal = 'myVariable' ; private static $types_fields = array( 'myArray' => array( 'name' => $classGlobal, 'age' => 'twenty five') )

我有以下PHP类:

class myClass extends AJAX_Callable {

public static $classGlobal = 'myVariable' ;

private static $types_fields = array(

        'myArray' => array(

            'name' => $classGlobal,

             'age' => 'twenty five')
    );
}
但这不起作用

来自:

类成员变量称为“属性”。您也可能会看到它们使用其他术语,如“属性”或“字段”,但在本参考中,我们将使用“属性”。它们是通过使用关键字public、protected或private中的一个来定义的,后跟一个普通变量声明。此声明可能包括一个初始化,但此初始化必须是一个常量值——也就是说,它必须能够在编译时进行计算,并且必须不依赖于运行时信息才能进行计算


我能想到的唯一方法就是把东西移到构造函数中。但这仍然是一件非常奇怪的事情。使用
静态
没有任何意义,因为您不能以常规方式使用它

class myClass extends AJAX_Callable {

    public static $classGlobal;
    private static $types_fields;

    public function __construct() {
        // parent::__construct(); // if you have one

        $this->classGlobal = 'myVariable';

        $this->types_fields = array(
        'myArray' => array(
            'name' => $this->classGlobal,
             'age' => 'twenty five')
        );
    }

    public function getTypes() {
        return $this->types_fields;
    }
}



$foo = new myClass();

var_dump($foo->getTypes());

据我所知,这在PHP中是不可能的。不能在类属性中使用表达式。将不起作用。甚至没有意义。