douxian1892 2015-03-12 13:55
浏览 110
已采纳

php oop调用私有方法

I have a class that look like this

class a {

    private $one;

    private function abc() {

    $this->one = "I am a string";
    return $this;
}

$call = new a;
$call->abc()->other_function();

As I was doing matutor method, php has caught a fatal error on calling function abc(). It said Call to private method xxx from context.

What I know of oop is very new, that private method/property can only be used within the same class.However, I cannot call the abc() even it is within the same class.

How come?

  • 写回答

2条回答 默认 最新

  • drlnwji79147769 2015-03-12 13:57
    关注

    Because you are not calling the method inside the class you are doing so outside the class code.

    $call = new a;
    $call->abc()->other_function();
    

    this is outside the context of the class, and this is why you get a Fatal Error.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?