PHP static properties and Static methods

Source: Internet
Author: User

php static Properties and Static methods2012-09-29 10:18 1518 people read comments (0) favorite reports Phpfunction

This PHP tutorial is primarily about using methods and basic examples of static and static methods in PHP.

· Static Properties

A static property means that its value retains its value, such as instantiating n objects in a class , you can define a static property in the constructor to remember the number of objects. The static and static variables in a class are about the same, except that they seem to have a few more restrictions on use in the class. Let's take a look at the general variables:

  1. <?php
  2. function Test () {
  3. $n = 1;
  4. echo "The number is: $n <br/>";
  5. $n + +;
  6. }
  7. Test ();
  8. Test ();
  9. Test ();
  10. Test ();
  11. Test ();
  12. Test ();
  13. ?>

<?phpfunction Test () {$n = 1; echo "The number is: $n <br/>"; $n + +;} Test (); test (); test (); test (); test (); test (); >

Obviously the result of this function is as follows:

The number is:1

The number is:1

The number is:1

The number is:1

The number is:1

The number is:1

But if your program is like this:

  1. <?php
  2. function Test () {
  3. static $n = 1;
  4. echo "The number is: $n <br/>";
  5. $n + +;
  6. }
  7. Test ();
  8. Test ();
  9. Test ();
  10. Test ();
  11. Test ();
  12. Test ();
  13. ?>

<?phpfunction
Test () {static $n = 1; echo "The number is: $n <br
/> "; $n + +;} Test (); test (); test (); test (); test (); test (); >

We just add a static keyword to the variable name , and the result is vastly different:

The number is:1

The number Is:2

The number Is:3

The number Is:4

The number Is:5

The number Is:6

1.static keywords can be used to modify variables, methods ( static methods)

2. without instantiation, you can directly access static properties and static methods in the class .

The properties and methods of 3.static can only access static properties and methods , and cannot access non-static properties and methods. Because static properties and methods are created, there may not be any instances of this class that can be called.

4. in the current class, if you want to access a static member, you can access it using the self:: keyword.

5. in a class we cannot use this key to access a static property because the static property already exists before the object may have been instantiated.

6. Static properties in a class are accessed statically by using the class name:: Static property name to invoke the static property in the class.

· Static Methods

  1. <?php
  2. Class Test {
  3. private static $money = 2000;
  4. public static function Getonemon () {
  5. Return test:: $money;
  6. }
  7. public static function Gettwomon () {
  8. Self:: $money = self:: $money-1500;
  9. Return self:: $money;
  10. }
  11. }
  12. echo "My balance now is:";
  13. echo Test:: Getonemon ();
  14. echo "<br>";
  15. echo "After consumption, my balance is:";
  16. echo Test:: Gettwomon ();
  17. ?>
  1. <?php
  2. Class Test {
  3. private static $money = 2000;
  4. public static function Getonemon () {
  5. Return test:: $money;
  6. }
  7. public static function Gettwomon () {
  8. Self:: $money = self:: $money-1500;
  9. Return self:: $money;
  10. }
  11. }
  12. echo "I now balance:"; Echo Test:: Getonemon (); echo "<br>";
  13. echo "After consumption, my balance is:"; Echo Test:: Gettwomon ();
  14. ?>

In this example, we see two ways to access the value of the static property $money : One is the class name mentioned earlier: The form of the property value, and the other is the use of the self keyword. Of course it is recommended to use the Self keyword this way, because if the day is not happy, we modify the class name, then if you use the first way, you do not have to modify the way to use it, of course, you have to in the same class, if you want to call the parent class in the subclass of static properties and methods, Then you have to use the parent:: way.

Say it again.

1: If you want to invoke other static methods in a static method, use the method: Class Name:: Method name in the form of call, or that sentence, if you make such a call in the same class, use the SELFT keyword to invoke it. Otherwise, you have to get the program will be an error.

static methods in 2:php cannot invoke non-static and non-static methods, nor can they use the class name:: Or self:: call a non-static property. You cannot use the $this-> property name to invoke, in short, static properties or methods can only be called in a static method, non-static cannot be called.

PHP static properties and Static methods

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.