PHP Calling a static function -
given following code looking pro's , con's of calling $this->mystaticfunc(); vs self::mystaticfunc();
class myclass private function myprivatefunc() { ... $this->mystaticfunc(); // or self::mystaticfunc(); ... } // no need tell me can't use $this in here public static function mystaticfunc() { ... } } // access static function myclass::mystaticfunc();
the cons of using $this->mystaticfunction()
are:
- it doesn't indicate intend (
->
indicates doing object, but aren't), - it may wind calling didn't expect,
- it confuse reader (all reader able determine sure author doesn't understand basic oo principles),
- and it's wrong: static functions belong classes, not objects. call them through class (including
self
), not object.
the possible pro may want override static function in child class, indicates function belongs object , not class. in case more appropriate use instance method.
Comments
Post a Comment