Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/280.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_Class_Static - Fatal编程技术网

PHP:在基类静态方法中获取调用方子类

PHP:在基类静态方法中获取调用方子类,php,class,static,Php,Class,Static,我有以下课程: class Base { static $static_var = 'base_static'; public static function static_init() { // HERE, I want to get the caller extended class. echo __CLASS__.'<br/>'; // HERE, I want to get the caller extended

我有以下课程:

class Base {
    static $static_var = 'base_static';
    public static function static_init() {
        // HERE, I want to get the caller extended class.
        echo __CLASS__.'<br/>';
        // HERE, I want to get the caller extended static variable.
        echo static::$static_var.'<br/>';
        // Do some initialization works depends on the static_var.
        // ...
    }
};

class Children extends Base {
    // overridden
    static $static_var = 'extended_static';
};

// Call Now
Children::static_init();

/** echos:
Base
base_static
*/

/** I want to export:
Children
extended_static
*/
类基{
静态$static_var='base_static';
公共静态函数static_init(){
//在这里,我想获得调用方扩展类。
回声类;
//这里,我想获取调用者扩展的静态变量。
echo static::$static_var.
; //是否进行一些初始化工作取决于静态变量。 // ... } }; 班级儿童扩展基础{ //凌驾 静态$static_var='extended_static'; }; //现在打电话 Children::static_init(); /**回声: 基础 基本静态 */ /**我想出口: 儿童 扩展静态 */
我可以将基类扩展到许多子类

因此,在我的子类中,我可以通过自己的静态变量定义静态参数


有没有办法做到这一点?或者我应该如何设计我的类?

我认为您需要的是“get_called_class()”,正如php.net对get_called_class()的定义:

“获取调用静态方法的类的名称。”

因此,最终代码如下所示:

class Base {
  static $static_var = 'base_static';
  public static function static_init() {
    $caller_class = get_called_class();
    // HERE, I want to get the caller extended class.
    echo $caller_class . '<br/>';
    // HERE, I want to get the caller extended static variable.
    echo $caller_class::$static_var.'<br/>';
    // Do some initialization works depends on the static_var.
    // ...
  }
};
类基{
静态$static_var='base_static';
公共静态函数static_init(){
$caller_class=get_called_class();
//在这里,我想获得调用方扩展类。
echo$caller_类。“
”; //这里,我想获取调用者扩展的静态变量。 echo$caller_类::$static_var.“
”; //是否进行一些初始化工作取决于静态变量。 // ... } };

我认为您需要的是“get_called_class()”,正如php.net对get_called_class()的定义:

“获取调用静态方法的类的名称。”

因此,最终代码如下所示:

class Base {
  static $static_var = 'base_static';
  public static function static_init() {
    $caller_class = get_called_class();
    // HERE, I want to get the caller extended class.
    echo $caller_class . '<br/>';
    // HERE, I want to get the caller extended static variable.
    echo $caller_class::$static_var.'<br/>';
    // Do some initialization works depends on the static_var.
    // ...
  }
};
类基{
静态$static_var='base_static';
公共静态函数static_init(){
$caller_class=get_called_class();
//在这里,我想获得调用方扩展类。
echo$caller_类。“
”; //这里,我想获取调用者扩展的静态变量。 echo$caller_类::$static_var.“
”; //是否进行一些初始化工作取决于静态变量。 // ... } };

我认为您需要的是“get_called_class()”,正如php.net对get_called_class()的定义:

“获取调用静态方法的类的名称。”

因此,最终代码如下所示:

class Base {
  static $static_var = 'base_static';
  public static function static_init() {
    $caller_class = get_called_class();
    // HERE, I want to get the caller extended class.
    echo $caller_class . '<br/>';
    // HERE, I want to get the caller extended static variable.
    echo $caller_class::$static_var.'<br/>';
    // Do some initialization works depends on the static_var.
    // ...
  }
};
类基{
静态$static_var='base_static';
公共静态函数static_init(){
$caller_class=get_called_class();
//在这里,我想获得调用方扩展类。
echo$caller_类。“
”; //这里,我想获取调用者扩展的静态变量。 echo$caller_类::$static_var.“
”; //是否进行一些初始化工作取决于静态变量。 // ... } };

我认为您需要的是“get_called_class()”,正如php.net对get_called_class()的定义:

“获取调用静态方法的类的名称。”

因此,最终代码如下所示:

class Base {
  static $static_var = 'base_static';
  public static function static_init() {
    $caller_class = get_called_class();
    // HERE, I want to get the caller extended class.
    echo $caller_class . '<br/>';
    // HERE, I want to get the caller extended static variable.
    echo $caller_class::$static_var.'<br/>';
    // Do some initialization works depends on the static_var.
    // ...
  }
};
类基{
静态$static_var='base_static';
公共静态函数static_init(){
$caller_class=get_called_class();
//在这里,我想获得调用方扩展类。
echo$caller_类。“
”; //这里,我想获取调用者扩展的静态变量。 echo$caller_类::$static_var.“
”; //是否进行一些初始化工作取决于静态变量。 // ... } };