Use and difference of static and non-static methods in PHP

Source: Internet
Author: User
Static keywords are used to modify attributes and methods. They are called static attributes and static methods. The static keyword declares that an attribute or method is related to a class, rather than a specific instance of the class. Therefore, this type of attribute or method is also called a class attribute or class method if access control... "> <LINKhref =" http://www.php100.com//statics/style/h

Static keywords are used to modify attributes and methods. They are called static attributes and static methods.

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 when static attributes and methods are created, there may not be any instances of this class that can be called

.

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

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

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.

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"
";
Unset ($ user3 );
Echo "now here have". $ user1-> getCount (). "user ";
?>

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

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
";
Echo Math: $ pi * $ r;
Echo"

";
// 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
";
Echo Math: $ pi * $ r;
?>


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. Referencing concepts in Java should also be Universal

. Static attributes and methods, which are created when a 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

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

View plaincopy to clipboardprint?
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"
";
Echo Math: Max ($ a, $ B );
Echo"
";
Echo"
";
Echo"
";
$ A = 99;
$ B = 100;
Echo "show that the maximum values in $ a and $ B are ";
Echo"
";
Echo Math: Max ($ a, $ B );
?>

How to call static methods
In the first example, when a static method calls other static methods, use self ::

// 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"
";
Echo Math: Max3 ($ a, $ B, $ c );
?>



Static method call static attributes
Use self: To call the static attributes of this class.

//
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 );
?>


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

// This method is incorrect
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 );
?>


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

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

// Math class for maximum value comparison.
Class Math
{         
Public function Max ($ num1, $ num2 ){
Echo "bad
";
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"
";
Echo Math: Max3 ($ a, $ B, $ c); // an error will be returned if this is the same.
?>

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

// 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"
";
Echo Math: Max3 ($ a, $ B, $ c );
?>

From PHP100 Forum http://bbs.php100.com.

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.