开发者

how to get_class() with scope resolution operator in PHP?

开发者 https://www.devze.com 2023-03-18 05:41 出处:网络
Example: <?php class X { 开发者_如何学Pythonfunction foo() { echo \"Class Name:\".get_class($this).\"<br>\";

Example:

<?php
class X {
  开发者_如何学Python  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();

?>


You'll have to store the class name in a variable and then use that.

$class = get_class($this);
echo $class::$private_var;
0

精彩评论

暂无评论...
验证码 换一张
取 消