开发者

Unusual OO behavior?

here is some php code:

class A {
  private function action(){
    echo 1;
  }
  public static function callAction(A $a){
    $a->action();
  }
}

$a = new A;
A::callAction($a);

can someone explain开发者_如何转开发 me why does object method is vissible from static method context how does following code works in other languages ???


The keyword private means the function is accessible from within this class only, not from within this object. The behaviour is the same in all languages I know.


As your $a object is passed as a parameter in A::callAction(), you can call any of its methods, static or not.

And as in A::callAction(A) you are in the implementation of A class, you can call $a->action.

Simple ? =)

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜