【问题标题】:How to determine the way of method calling from child class如何确定从子类调用方法的方式
【发布时间】:2013-03-01 10:54:01
【问题描述】:

请帮助我。 例如,我有一个类 Foo,它从类 Bar 扩展而来。

    class Bar
    {
          public function __call($func, $args){
               echo "Calling method {$func}";
          }
          public function __callstatic($func, $args){
               echo "Calling static method {$func}";
          }
          public function run(){
               echo "calling Bar::run method \n";
          }
    }
    class Foo extends Bar
    {
          public $objBar;
          public function __construct(){
              $this -> objBar = new Bar();
          }
          public function callViaObject(){
              $this -> objBar -> run();
              $this -> objBar -> run1();
          }
          public function callViaParent(){
              parent::run();
              parent::run1();
          }
    }
    $foo = new foo();
    $foo -> callViaObject(); 
    /* output 
       calling Bar::run method \n
       Calling method run1; */
    $foo -> callViaParent();
    /* output 
       calling Bar::run method \n
       Calling method run1; !! */

这里有个问题,当我从子类调用parent::的方法,而父类有对象时,调用方法parent::不是静态调用。

那么我如何签入Bar 类,如何调用方法? 我可以检查调用类型是 parent:: 吗?

非常感谢大家!!

【问题讨论】:

  • 您是否尝试过将__callstatic 更改为__callStatic(注意大写S)?这可能是原因。
  • 我试过了,还是一样。

标签: php class scope


【解决方案1】:

在你的 A 类中添加 private static function run1() {}

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2023-03-29
  • 2011-12-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-11-09
  • 2011-10-24
相关资源
最近更新 更多