Use and definition of PHP static methods and static attributes

Source: Internet
Author: User
This PHP tutorial mainly describes how to use static attributes and static methods in PHP and provides basic examples.
Static attributes mean that their values keep their values. For example, if n objects are instantiated in the class, you can define a static attribute in the constructor to remember the number of objects. The static attributes in the class are similar to those in the static variables, but the class seems to have some usage restrictions. Let's take a look at the common variables:
Copy Code Print?

<? PHP

Function testnotstaticfunc (){
$ N = 1;
Echo "the number is $ n <br/> ";
$ N ++;
}
Testnotstaticfunc ();
Testnotstaticfunc ();
Testnotstaticfunc ();
Testnotstaticfunc ();
Testnotstaticfunc ();
Testnotstaticfunc ();

?>

Obviously, the result of this function is as follows:

This number is: 1
This number is: 1
This number is: 1
This number is: 1
This number is: 1
This number is: 1

But if yourProgramYes:
Copy code to print?

<? PHP
Function teststaticfunc (){
Static$ N = 1;
Echo "the number is $ n <br/> ";
$ N ++;
}
Teststaticfunc ();
Teststaticfunc ();
Teststaticfunc ();
Teststaticfunc ();
Teststaticfunc ();
Teststaticfunc ();
?>

We just added a static keyword to the variable name, and the results were quite different:

This number is: 1
This number is: 2
This number is: 3
This number is: 4
This number is: 5
The number is 6.

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

2. Without instantiation, the static attributes and static methods in the examples class can be directly used.

3. for static attributes and methods, you can only access static attributes and methods, but not non-static attributes and methods. Because no instance of this class can be called when static attributes and methods are created.

4. You can use the self: keyword to access static members in the current class.

5. In the class, we cannot use this key to access static attributes, because static attributes already exist before the object may not be instantiated.

6. Access static attributes in the static method of the class. Use the Class Name: static attribute name to call static attributes in the class.

To sum up, the example copied from the Web tutorial site is very good. Of course, the above content is from the Web tutorial site. I just sorted it out.
Copy code to print?

<? PHP
Class test {
Private Static $ money = 2000;
Public static function getonemon (){
Return test: $ money;
}
Public static function gettwomon (){
SELF: $ money = self: $ money-1500;
Return self: $ money;
}
}
Echo "my current balance is :";
Echo test: getonemon ();
Echo "<br> ";
Echo "after consumption, My balance is :";
Echo test: gettwomon ();
?>

In this example, we can see that two methods are used to access the value of the static attribute $ money: one is the class name mentioned above: the form of the attribute value, in addition, the self keyword is used. Of course, we recommend using the self keyword, because if you are not happy that day and we modify the class name, if you use the first method, do you have to modify the method to call it? Of course, you have to be in the same class. If you want to call the static attributes and methods of the parent class in the subclass, you have to use the parent: method. For other advanced applications, I think we will certainly encounter the source code in WordPress. Let's talk about it later. As a newbie, the example I have given is really difficult, so I borrowed the example on the online tutorial site. I'm afraid this habit will continue in the future. This is actually accelerating our learning process, because we are standing on the shoulders of giants. You can see a higher level. If you want to get farther away, go to the first floor!

Let's talk about 1: If you want to call other static methods in a static method, use the following method: Class Name: method name to call the method, if you make such a call in the same class, use the selft keyword for the call. Otherwise, your program may have to report an error.

NOTE 2: static methods in PHP cannot call non-static attributes or non-static methods, or use the Class Name: Or self: to call non-static attributes. You cannot use the $ this-> attribute name to call a static property or method.

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.