2011-07-09 15 views
1

例:PHPでスコープ解決演算子を使用して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(); 

?> 
+0

何か助けてください、なぜ私はget_class()と(::)演算子を使用できません。 – hardik

答えて

0

あなたは変数にクラス名を格納し、それを使用する必要があります。

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

答えはありませんが、動作しません。 :( – hardik

関連する問題