【发布时间】:2011-10-07 12:45:44
【问题描述】:
<?php
class X {
function foo() {
echo "Class Name:".get_class($this)."<br>"; //it displays Y... :)
echo get_class($this)::$public_var; //not working
echo Y::$public_var; //works
Y::y_method(); //works
get_class($this)::y_method(); //not working
$classname = get_class($this);
$classname::y_method(); // again not working.. :(
}
function bar() {
$this->foo();
}
}
class Y extends X {
public static $public_var = "Variable of Y Class";
public function y_method()
{
echo "Y class method";
}
}
$y = new Y();
$y->bar();
?>
my only question is how to get access members of y class only with dynamically providing class name without changing current structure.
【问题讨论】:
-
get_class() 将始终返回 X。$this 未在静态函数中设置。启用错误并阅读日志以了解。您可能正在寻找
get_called_class()。 -
Thanx binaryLV 我已更改变量名。
-
hakre -> get_class() 正在返回类名 Y,请检查自己