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进行覆盖