Using $this, self::, parent:: for code readability

前端 未结 6 721
伪装坚强ぢ
伪装坚强ぢ 2021-02-08 13:51

I would like to know if it is acceptable/preferred to use self::method() and parent::method() when working in php classes.

You can use $this->method() but $this-> can al

6条回答
  •  情话喂你
    2021-02-08 14:17

    There is a difference.

    $this refers to an instance of an object.

    parent and self are used to call methods statically.

    This page of PHP's manual explains it in better detail than I have time to write at the moment. The first example in particular should help to highlight some of the differences. I encourage you to copy paste the first example and mess about with it, as I think its an important concept to get in your mind if you don't already know the difference.

提交回复
热议问题