Wednesday 15 May 2013

PHP Private Static Function -



PHP Private Static Function -

i junior php programmer. still have lot learn. that's why inquire question. in class have public function can phone call outside class. have private function can phone call several times in class private function resides, reusable purpose. set private function static , phone call function with:

self::privatefunctionname();

by using self reminds me private function resides in class. if utilize $this->privatefunctionname() non-static function, in superclass/base class or in subclass itself. why utilize static private function. in professional point of view, thought utilize static private function instead of non-static? there disadvantage professional programmer prefers avoid static function?

only using self::... must not mean method static. parent:: , self:: work non-static methods. can find in php manual - scope resolution operator (::) , add together exemplary code excerpt @ end of answer.

you perhaps might want read through answers of before question:

when utilize self vs $this?

in total there more details short description in answer.

you might have been confused scope-resolution-operator :: used those. had similar understanding problem grasping that.

however, not take utilize static methods such limited reason. static class methods should used in limited , narrowed situations. rule of thumb:

"do not utilize static class methods."

if start object oriented programming, utilize normal object methods.

here excerpt existing code shows self:: parent:: used standard (non-static) methods:

<?php ... /** * class xmlelementiterator * * iterate on xmlreader element nodes */ class xmlelementiterator extends xmlreaderiterator { private $index; private $name; private $didrewind; /** * @param xmlreader $reader * @param null|string $name element name, leave empty or utilize '*' elements */ public function __construct(xmlreader $reader, $name = null) { parent::__construct($reader); $this->setname($name); } /** * @return void */ public function rewind() { parent::rewind(); $this->ensurecurrentelementstate(); $this->didrewind = true; $this->index = 0; } /** * @return xmlreadernode|null */ public function current() { $this->didrewind || self::rewind(); $this->ensurecurrentelementstate(); homecoming self::valid() ? new xmlreadernode($this->reader) : null; } ...

php static-methods static-function

No comments:

Post a Comment