PHP object-oriented Journey: a deep understanding of static variables and Methods

Source: Internet
Author: User

The static keyword declares that an attribute or method is related to a class, rather than a specific instance of the class. Therefore, such attributes or methods are also called "class attributes" or "class methods ".

If the access control permission permits, you can directly use the class name with two colons ":" instead of creating the class object.

Static keywords can be used to modify variables and methods.

Without instantiation, you can directly invoke the static attributes and static methods in the class.

Static attributes and methods can only access static attributes and methods, but cannot access non-static attributes and methods. Because no instance of this class can be called when static attributes and methods are created.

Static attributes, which are shared by all instances in the memory.

Use the self: keyword to access the static members of the current class.
Public characteristics of static attributes

All instances of a class share static attributes of the class.

That is to say, even if there are multiple instances in the memory, there is only one static attribute.

In the following example, a counter $ count attribute is set to private and static. In this way, you cannot directly access the $ count attribute. The running result shows that multiple instances use the same static $ count attribute.

Copy codeThe Code is as follows:
<?
Class user {
Private static $ count = 0; // records the logon status of all users.
Public function _ construct (){
Self: $ count = self: $ count + 1;
}
Public function getCount (){
Return self: $ count;
}
Public function _ destruct (){
Self: $ count = self: $ count-1;
}
}
$ User1 = new user ();
$ User2 = new user ();
$ User3 = new user ();
Echo "now here have". $ user1-> getCount (). "user ";
Echo "<br> ";
Unset ($ user3 );
Echo "now here have". $ user1-> getCount (). "user ";
?>

Program running result:
1
2

Now here have 3 user
Now here have 2 user jb51.net
Direct call of static attributes

Static attributes can be directly used without instantiation. They can be used directly before a class is created.

The method is Class Name: static property name.
Copy codeThe Code is as follows:
<?
Class Math {
Public static $ pi = 3.14;

}
// Calculate the area of the garden with a radius of 3.
$ R = 3;
Echo "the area with a radius of $ r is <br> ";
Echo Math: $ pi * $ r;

Echo "<br> ";
// Here I think 3.14 is not accurate enough, so I set it more accurately.
Math: $ pi = 3.141592653589793;
Echo "the area with a radius of $ r is <br> ";
Echo Math: $ pi * $ r;
?>

Program running result:
1
2
3
4

The area with a radius of 3 is
28.26
The area with a radius of 3 is
28.2743338823

Class is not created, and static attributes can be directly used. When will the static property be created in the memory? No relevant information is found in PHP. By referencing the concepts in Java, it should also be universal.

Static attributes and methods, which are created when a class is called. Class is called, which means the class is created or any static member in the class is called.
Static Method

Static methods can be directly used without the class being instantiated.

The method is Class Name: static method name.

Next we will continue to write this Math class for mathematical computation. We designed a method to calculate the maximum value. Since it is a mathematical operation, we do not need to instantiate this class. If this method can be used, it will be much more convenient.

We only designed this class to demonstrate the static method. The max () function is provided in PHP to compare values.

Copy codeThe Code is as follows:
<?
Class Math {

Public static function Max ($ num1, $ num2 ){
Return $ num1> $ num2? $ Num1: $ num2;
}
}
$ A = 99;
$ B = 88;
Echo "show that the maximum values in $ a and $ B are ";
Echo "<br> ";
Echo Math: Max ($ a, $ B );
Echo "<br> ";
$ A = 99;
$ B = 100;
Echo "show that the maximum values in $ a and $ B are ";
Echo "<br> ";
Echo Math: Max ($ a, $ B );
?>

Program running result:

The maximum value in $ a and $ B is
99
The maximum value in $ a and $ B is
100
How to call static methods

In the first example, when a static method calls other static methods, the class name is directly used.

Copy codeThe Code is as follows:
<?
// Math class for maximum value comparison.
Class Math {

Public static function Max ($ num1, $ num2 ){
Return $ num1> $ num2? $ Num1: $ num2;
}
Public static function Max3 ($ num1, $ num2, $ num3 ){
$ Num1 = Math: Max ($ num1, $ num2 );
$ Num2 = Math: Max ($ num2, $ num3 );
$ Num1 = Math: Max ($ num1, $ num2 );
Return $ num1;
}
}
$ A = 99;
$ B = 77;
$ C = 88;
Echo "show the maximum value in $ a $ B $ c is ";
Echo "<br> ";
Echo Math: Max3 ($ a, $ B, $ c );
?>

Program running result:
1
2

The maximum value shown in 99 77 88 is
99

You can also use self: to call other static methods in the current class. (Recommended)

Copy codeThe Code is as follows:
<?
// Math class for maximum value comparison.
Class Math {

Public static function Max ($ num1, $ num2 ){
Return $ num1> $ num2? $ Num1: $ num2;
}
Public static function Max3 ($ num1, $ num2, $ num3 ){
$ Num1 = self: Max ($ num1, $ num2 );
$ Num2 = self: Max ($ num2, $ num3 );
$ Num1 = self: Max ($ num1, $ num2 );
Return $ num1;
}
}
$ A = 99;
$ B = 77;
$ C = 88;
Echo "show the maximum value in $ a $ B $ c is ";
Echo "<br> ";
Echo Math: Max3 ($ a, $ B, $ c );
?>

Program running result:
1
2

The maximum value shown in 99 77 88 is
99
Static method call static attributes

Use Class Name: static property name to call static properties in this class.
Copy codeThe Code is as follows:
<?
//
Class Circle {
Public static $ pi = 3.14;

Public static function circleAcreage ($ r ){
Return $ r * Circle: $ pi;
}
}
$ R = 3;
Echo "The area of the Circle with the radius $ r is". Circle: circleAcreage ($ r );
?>

Program running result:
1

The area of the circle with a radius of 3 is 28.26.

Use self: to call the static attributes of this class. (Recommended)

Copy codeThe Code is as follows:
<?
//
Class Circle {
Public static $ pi = 3.14;

Public static function circleAcreage ($ r ){
Return $ r * self: $ pi;
}
}
$ R = 3;
Echo "The area of the Circle with the radius $ r is". Circle: circleAcreage ($ r );
?>

Program running result:
1

The area of the circle with a radius of 3 is 28.26.
Static methods cannot call non-static attributes.

Static methods cannot call non-static attributes. You cannot use self: to call non-static attributes.

Copy codeThe Code is as follows:
<?
//
Class Circle {
Public $ pi = 3.14;

Public static function circleAcreage ($ r ){
Return $ r * self: pi;
}
}
$ R = 3;
Echo "The area of the Circle with the radius $ r is". Circle: circleAcreage ($ r );
?>

Program running result:
1

Fatal error: Undefined class constant 'pi' in E: PHPProjectstest. php on line 7

You cannot use $ this to obtain non-static attribute values.

Copy codeThe Code is as follows:
<?
//
Class Circle {
Public $ pi = 3.14;

Public static function circleAcreage ($ r ){
Return $ r * $ this-> pi;
}
}
$ R = 3;
Echo "The area of the Circle with the radius $ r is". Circle: circleAcreage ($ r );
?>

Program running result:
1

Fatal error: Using $ this when not in object context in E: PHPProjectstest. php on line 7
Static method call non-static method

In PHP5, you cannot use $ this to call non-static methods in static methods.

Copy codeThe Code is as follows:
<?
// Math class for maximum value comparison.
Class Math {
Public function Max ($ num1, $ num2 ){
Echo "bad <br> ";
Return $ num1> $ num2? $ Num1: $ num2;
}
Public static function Max3 ($ num1, $ num2, $ num3 ){
$ Num1 = $ this-> Max ($ num1, $ num2 );
$ Num2 = $ this-> Max ($ num2, $ num3 );
$ Num1 = $ this-> Max ($ num1, $ num2 );
Return $ num1;
}
}
$ A = 99;
$ B = 77;
$ C = 188;
Echo "show the maximum value in $ a $ B $ c is ";
Echo "<br> ";
Echo Math: Max3 ($ a, $ B, $ c );
?>

Program running result:

The maximum value shown in 99 77 188 is
Fatal error: Using $ this when not in object context in E: test. php on line 10

When a class has a non-static method called by self:, the system automatically converts this method to a static method.

The following code is executed and has results. Because the Max method is converted to a static method by the system.

Copy codeThe Code is as follows:
<?
// Math class for maximum value comparison.
Class Math {
Public function Max ($ num1, $ num2 ){
Return $ num1> $ num2? $ Num1: $ num2;
}
Public static function Max3 ($ num1, $ num2, $ num3 ){
$ Num1 = self: Max ($ num1, $ num2 );
$ Num2 = self: Max ($ num2, $ num3 );
$ Num1 = self: Max ($ num1, $ num2 );
Return $ num1;
}
}
$ A = 99;
$ B = 77;
$ C = 188;
Echo "show the maximum value in $ a $ B $ c is ";
Echo "<br> ";
Echo Math: Max3 ($ a, $ B, $ c );
?>

Program running result:
1
2

The maximum value shown in 99 77 188 is
188

In the following example, we have made the static method Max3 use self: the non-static method Max is called, and the non-static method Max calls the non-static attribute $ pi through $ this.

An error is reported during running. This error is the same as that in the previous example 3-1-9.php. In this case, the non-static method Max reports the non-static attribute error of the static method call.

This proves that the non-static method Max defined here is automatically converted to a static method by the system.

Copy codeThe Code is as follows:
<?
// Math class for maximum value comparison.
Class Math {
Public $ pi = 3.14;

Public function Max ($ num1, $ num2 ){
Echo self: $ pi; // the call here does not seem to be a problem.
Return $ num1> $ num2? $ Num1: $ num2;
}
Public static function Max3 ($ num1, $ num2, $ num3 ){
$ Num1 = self: Max ($ num1, $ num2 );
$ Num2 = self: Max ($ num2, $ num3 );
$ Num1 = self: Max ($ num1, $ num2 );
Return $ num1;
}
}
$ A = 99;
$ B = 77;
$ C = 188;
Echo "show the maximum value in $ a $ B $ c is ";
Echo "<br> ";
Echo Math: Max3 ($ a, $ B, $ c );
?>

Program running result:
1
2

The maximum value shown in 99 77 188 is
Fatal error: Access to undeclared static property: Math: $ pi in E: PHPProjectstest. php on line 7

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.