开发者

Unusual OO behavior?

开发者 https://www.devze.com 2022-12-20 00:56 出处:网络
here is some php code: class A { private function action(){ echo 1; } public static function callAction(A $a){

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

精彩评论

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