How to call grandparent method without getting E_STRICT error?

后端 未结 4 1154
有刺的猬
有刺的猬 2021-01-04 02:57

Sometimes I need to execute grandparent method (that is, bypass the parent method), I know this is code smell, but sometimes I can\'t change the other classes (frameworks, l

4条回答
  •  清歌不尽
    2021-01-04 03:15

    Timo's answer works but I think it works by accident more than by design. If you look at the opcodes for a $this->doX vs a parent::doX vs a Grandparent::doX you can see that Grandparent::doX is invoked as a static method but PHP will use the $this that's in scope

    $this->doX
      17     1        EXT_STMT                                                 
             2        INIT_METHOD_CALL                               'doX'
             3        EXT_FCALL_BEGIN                                          
             4        DO_FCALL                                      0          
             5        EXT_FCALL_END                                            
    
    parent::doX
      18     6        EXT_STMT                                                 
             7        FETCH_CLASS                                 514  :1      
             8        INIT_STATIC_METHOD_CALL                     $1, 'doX'
             9        EXT_FCALL_BEGIN                                          
            10        DO_FCALL                                      0          
            11        EXT_FCALL_END                                            
    
    Grandparent::doX
      19    12        EXT_STMT                                                 
            13        INIT_STATIC_METHOD_CALL                  'C1', 'doX'
            14        EXT_FCALL_BEGIN                                          
            15        DO_FCALL                                      0          
            16        EXT_FCALL_END                                            
    

    The $this parameter is available in the Grandparent's static invocation because of this (from https://www.php.net/manual/en/language.oop5.basic.php):

    The pseudo-variable $this is available when a method is called from within an object context. $this is a reference to the calling object (usually the object to which the method belongs, but possibly another object, if the method is called statically from the context of a secondary object). As of PHP 7.0.0 calling a non-static method statically from an incompatible context results in $this being undefined inside the method. Calling a non-static method statically from an incompatible context has been deprecated as of PHP 5.6.0. As of PHP 7.0.0 calling a non-static method statically has been generally deprecated (even if called from a compatible context). Before PHP 5.6.0 such calls already triggered a strict notice.

提交回复
热议问题