如何在PHP中使用作用域解析运算符获取_class()?
例如:如何在PHP中使用作用域解析运算符获取_class()?,php,Php,例如: <?php class X { function foo() { echo "Class Name:".get_class($this)."<br>"; echo get_class($this)::$private_var; //not working echo Y::$private_var; //works Y::y_method(); //works get_class(
<?php
class X {
function foo() {
echo "Class Name:".get_class($this)."<br>";
echo get_class($this)::$private_var; //not working
echo Y::$private_var; //works
Y::y_method(); //works
get_class($this)::y_method(); //not working
}
function bar() {
$this->foo();
}
}
class Y extends X {
public static $private_var = "Variable of Y Class";
public function y_method()
{
echo "Y class method";
}
}
$y = new Y();
$y->bar();
?>
必须将类名存储在变量中,然后使用该变量
$class = get_class($this);
echo $class::$private_var;
有人请帮忙,为什么我不能使用带有(:)操作符的get_class()。