Use of static statically-keyword in PHP

Source: Internet
Author: User

PHP In addition to the use of general classes and methods, access control, there is static keyword statics, static variables can be local variables can also be global variables, when a program segment is completed, the static variable does not disappear, it still exists in memory, the next time when the definition or the previous value, Often used in recursive or sub-function to retain before the value, can be used to define variables and methods, the role is not specific to say, the use of the way, the following simple code:

1 classhuman{2      Public $name;3     protected $height;4 5      Public Static $sValue= "Static Value in Human";6      Public functionEat$food){7         Echo $this->name. "' s eating."$food. "\ n";8     }9 }Ten  One classMeextendsHuman A { -      Public $team= "LSL"; -     Private $age= "23"; the  -      Public Static $president= "Zzy"; -  -      Public Static functionChangepresident ($NEWPRSDT){ +         Static::$president=$NEWPRSDT; -         EchoParent::$sValue. "<br/>"; +     } A  at     //static variables and method operations -     Private Static $a= "ABC"; -  -      Public Static functionABC () { -         Echo"<br/>". Self::$a; -Self::$a. = "Def"; in     } - } to  + EchoMe::$president. "\ n"; -Me::changepresident ("Web"); the EchoMe::$president. "<br/>"; *  $ Panax Notoginseng  -Me::ABC (); theME::ABC ();

First define a human human, then define a class me to inherit the human, then the members inside the human can be used by the Me object, and the entire use of the value of the member variable is valid, According to the code can see: The value of the static variable is changed after the next call is changed, if it is a normal variable can not be implemented, only through the object change to be effective, and through the method inside the class can not be changed, through the last two lines of code more easily see, called 2 times method ABC after static variable $ A value changes, static variables to think about it can be understood, the use is very extensive, the following summary of several static keyword-related rules:

1. General static properties are used to hold the public data of the class

2, static methods can only access static properties, including the class and the parent class

3, static members do not need to instantiate the object can be accessed

4. Accessing static properties Inside this class is accessed with the self or the static keyword, and the variables on the back include $, for example: self: $a or static:: $a

5. Access the parent class static property using parent, such as: Parent:: $name

6. Access static variables or methods outside the class using the class name directly, without instantiation. such as: Me:: $pan and Me::abc ()

That's the point, that's all.

Use of static statically-keyword in PHP

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.