PHP static variables, members, Methods

Source: Internet
Author: User
1. Static variables exist only in the function scope. Note: These variables will not be lost after execution (the variable will still remember the original value when the function is called next time ).

To define static variables, you only need to add the static keyword before the variable.

2. A static member is a type variable that can be viewed as an instance of the entire class rather than a class. Unlike General instance variables, static members only retain one variable value, which is valid for all instances (all instances share this member ).

3. static method: the rules of static methods are the same as those of static variables. The static keyword can be used to mark a method as a static method. The static method can be accessed through the class name and scope qualifier (: :) (paamayim nekudow.im.
Java Code
  1. <? PHP
  2. Var_dump ("Hellow word! ");
  3. /** 
  4. * Test example 
  5. */
  6. ClassMyobject {
  7. Public Static$ Mystaticvar =0;// Declare a shared static variable mystaticvar and assign the initial value 0
  8. Function mymethod (){
  9. SELF: $ mystaticvar + =2;
  10. Echo self: $ mystaticvar."\ N";
  11. }
  12. StaticFunction mymethod2 (){
  13. SELF: $ mystaticvar + =2;
  14. Echo self: $ mystaticvar."\ N";
  15. }
  16. }
  17. ClassMytherobjectExtendsMyobject {
  18. Public Static$ Mystaticvar =0;// Rewrite mystaticvar
  19. Function mythermethod (){
  20. Echo parent: $ mystaticvar."\ N";// Parent access class
  21. Echo self: $ mystaticvar."\ N";// Self accesses the current class
  22. }
  23. }
  24. $ Instance1 =NewMyobject ();// Instantiate myobject
  25. $ Instance1-> mymethod ();
  26. $ Instance2 =NewMyobject ();
  27. $ Instance2-> mymethod ();
  28. $ Instane3 =NewMytherobject ();
  29. $ Instane3-> myothermethod ();
  30. Echo myobject: $ mymethod2 ();//
  31. ?>

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.