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

php:如何从继承的类中获取静态变量?

php:如何从继承的类中获取静态变量?,php,Php,代码如下: class Crud { public static function get($id); echo "select * from ".self::$table." where id=$id";// here is the problem } class Player extends Crud { public static $table="user" } Player::get(1); 我可以使用Player::$table,但Crud将在许多类中继承 有什么想法吗?

代码如下:

class Crud {
 public static function get($id);
 echo "select * from ".self::$table." where id=$id";// here is the problem
}

class Player extends Crud {
 public static $table="user"
}


Player::get(1);
我可以使用Player::$table,但Crud将在许多类中继承


有什么想法吗?

要在PHP中引用静态成员,有两个关键字:

  • self
    用于“静态”绑定(使用它的类)

  • static
    用于“动态”/“后期静态绑定”(“叶”类)


在您的例子中,您希望使用
static::$table

引用PHP中的静态成员,有两个关键字:

  • self
    用于“静态”绑定(使用它的类)

  • static
    用于“动态”/“后期静态绑定”(“叶”类)


在您的情况下,您想使用
静态::$table
您想使用
静态:

<?php
class Crud {
    public static $table="crud";
    public static function test() {
       print "Self: ".self::$table."\n";
       print "Static: ".static::$table."\n";
    }
}

class Player extends Crud {
    public static $table="user";
}

Player::test();

$ php x.php 
Self: crud
Static: user

您想使用
静态:

<?php
class Crud {
    public static $table="crud";
    public static function test() {
       print "Self: ".self::$table."\n";
       print "Static: ".static::$table."\n";
    }
}

class Player extends Crud {
    public static $table="user";
}

Player::test();

$ php x.php 
Self: crud
Static: user

当使用extends时,您可以为父类添加父类。确实有
父类
可以引用父类的classI miss进行覆盖您可以在使用extends时为父类添加父类。确实有
父类
可以引用父类的classI miss进行覆盖