PHP Object-oriented tour: Static variables and methods _php Tutorial

Source: Internet
Author: User
The static keyword declares that a property or method is related to a class and not to a particular instance of a class, so such a property or method is also referred to as a class property or class method.

If the access Control permission allows, you do not have to create the class object and directly use the class name plus two colons "::" Call.

The static keyword can be used to modify variables, methods.

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

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

Static property, only one copy in memory, shared for all instances.

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

All instances of a class, a static property in a common class.

That is, even if there are multiple instances in memory, there is only one copy of the static property.

The following example sets a counter $count property, setting the private and static adornments. This way, the $count property is not directly accessible to the outside world. And the results of the program run we also see multiple instances using the same static $count property.

Code to copy code as follows
Class user{
private static $count = 0; Logs 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;
}
}
$user 1 = new user ();
$user 2 = new User ();
$user 3 = new User ();
echo "Now here has". $user 1->getcount (). "User";
echo "
";
Unset ($user 3);
echo "Now here has". $user 1->getcount (). "User";
?>

Program Run Result:
1
2

Now here has 3 user
Now here you have 2 user bkjia.c0m
Static properties are called directly

Static properties can be used directly without instantiation and can be used directly when the class is not created.

The method used is the class name:: Static property name.

Code to copy code as follows

Class math{
public static $PI = 3.14;

}
Find the area of a garden with a radius of 3.
$r = 3;
echo "radius is the area of the $r is
";
echo Math:: $pi * $r * $R;

echo "

";
Here I think 3.14 is not accurate enough, I set it more precisely.
Math:: $pi = 3.141592653589793;
echo "radius is the area of the $r is
";
echo Math:: $pi * $r * $R;
?>

Program Run Result:
1
2
3
4

The radius is 3 of the area is
28.26
The radius is 3 of the area is
28.2743338823

Class is not created, static properties can be used directly. When was the static property created in memory? No data was found in PHP. Referring to the concepts in Java, the explanation should also be universal.

Static properties and methods that are created when the class is invoked. Class is called, meaning that the class is created or any static member in the class is called.
Static methods

Static methods do not require that the class be instantiated to be used directly.

The method used is the class name:: Static method name.

Let's continue writing this math class for mathematical calculations. We design a method to figure out the maximum value. Since it is a mathematical operation, we do not need to instantiate this class, if this method can be used to be more convenient.

We are just this class designed to demonstrate the static method. Www.111Cn.net provides the max () function comparison values in PHP.

Code to copy code as follows
Class math{

public static function Max ($num 1, $num 2) {
Return $num 1 > $num 2? $num 1: $num 2;
}
}
$a = 99;
$b = 88;
echo "shows the maximum value in $ A and B is";
echo "
";
Echo Math::max ($a, $b);
echo "
"; Echo"
"; Echo"
";
$a = 99;
$b = 100;
echo "shows the maximum value in $ A and B is";
echo "
";
Echo Math::max ($a, $b);
?>

Program Run Result:

Displays the maximum values in $ A and B are
99
Displays the maximum values in $ A and B are
100
How static methods Call static methods

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

Code to copy code as follows
The math class that implements the maximum value comparison.
Class math{

public static function Max ($num 1, $num 2) {
Return $num 1 > $num 2? $num 1: $num 2;
}
public static function Max3 ($num 1, $num 2, $num 3) {
$num 1 = Math::max ($num 1, $num 2);
$num 2 = Math::max ($num 2, $num 3);
$num 1 = Math::max ($num 1, $num 2);
return $num 1;
}
}
$a = 99;
$b = 77;
$c = 88;
echo "Displays the maximum value in the $a $b $c is";
echo "
";
Echo math::max3 ($a, $b, $c);
?>

Program Run Result:
1
2

Display the maximum value in 99 77 88 is
99

You can also use self:: Call other static methods in the current class. Recommendations

Code to copy code as follows
The math class that implements the maximum value comparison.
Class math{

public static function Max ($num 1, $num 2) {
Return $num 1 > $num 2? $num 1: $num 2;
}
public static function Max3 ($num 1, $num 2, $num 3) {
$num 1 = Self::max ($num 1, $num 2);
$num 2 = Self::max ($num 2, $num 3);
$num 1 = Self::max ($num 1, $num 2);
return $num 1;
}
}
$a = 99;
$b = 77;
$c = 88;
echo "Displays the maximum value in the $a $b $c is";
echo "
";
Echo math::max3 ($a, $b, $c);
?>

Program Run Result:
1
2

Display the maximum value in 99 77 88 is
99
static method call static property Www.111Cn.Net

Use the class name:: Static property name to invoke a static property in this class.

Code to copy code as follows
//
Class circle{
public static $PI = 3.14;

public static function Circleacreage ($r) {
return $r * $r * Circle:: $PI;
}
}
$r = 3;
echo "Radius $r the area of the circle is". Circle::circleacreage ($R);
?>

Program Run Result:
1

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

Use self:: Call the static property of this class. Recommendations

Code to copy code as follows
//
Class circle{
public static $PI = 3.14;

public static function Circleacreage ($r) {
return $r * $r * self:: $PI;
}
}
$r = 3;
echo "Radius $r the area of the circle is". Circle::circleacreage ($R);
?>

Program Run Result:
1

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

A static method cannot invoke a non-static property. You cannot use Self:: Call a non-static property.

Code to copy code as follows
//
Class circle{
Public $pi = 3.14;

public static function Circleacreage ($r) {
return $r * $r * self::p i;
}
}
$r = 3;
echo "Radius $r the area of the circle is". Circle::circleacreage ($R);
?>

Program Run Result:
1

Fatal error:undefined class constant ' pi ' in e:phpprojectstest.php on line 7

You cannot also use $this to get the value of a non-static property.

Code to copy code as follows

//
Class circle{
Public $pi = 3.14;

public static function Circleacreage ($r) {
return $r * $r * $this->pi;
}
}
$r = 3;
echo "Radius $r the area of the circle is". Circle::circleacreage ($R);
?>

Program Run Result:
1

Fatal error:using $this When not in object context in e:phpprojectstest.php on line 7
Static method calls a non-static method

In PHP5, a non-static method cannot be called using $this identity in a static method.

Code to copy code as follows

The math class that implements the maximum value comparison.
Class math{
Public Function Max ($num 1, $num 2) {
echo "Bad
";
Return $num 1 > $num 2? $num 1: $num 2;
}
public static function Max3 ($num 1, $num 2, $num 3) {
$num 1 = $this->max ($num 1, $num 2);
$num 2 = $this->max ($num 2, $num 3);
$num 1 = $this->max ($num 1, $num 2);
return $num 1;
}
}
$a = 99;
$b = 77;
$c = 188;
echo "Displays the maximum value in the $a $b $c is";
echo "
";
Echo math::max3 ($a, $b, $c);
?>

Program Run Result:

Display the maximum value in 99 77 188 is
Fatal error:using $this When not in object context in E:www.111cn.net test.php on line 10

When there is a non-static method in a class that is self: called, the system automatically converts the method to a static method.

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

Code to copy code as follows
The math class that implements the maximum value comparison.
Class math{
Public Function Max ($num 1, $num 2) {
Return $num 1 > $num 2? $num 1: $num 2;
}
public static function Max3 ($num 1, $num 2, $num 3) {
$num 1 = Self::max ($num 1, $num 2);
$num 2 = Self::max ($num 2, $num 3);
$num 1 = Self::max ($num 1, $num 2);
return $num 1;
}
}
$a = 99;
$b = 77;
$c = 188;
echo "Displays the maximum value in the $a $b $c is";
echo "
";
Echo math::max3 ($a, $b, $c);
?>

Program Run Result:
1
2

Display the maximum value in 99 77 188 is
188

In the following example, we let the static method Max3 use self:: called the non-static method Max, there is a non-static method max to call the non-static property $pi through $this.

In the run is reported error, this error and the previous example 3-1-9.php, this is the non-static method Max reported static method call non-static property error.

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

Code to copy code as follows

The math class that implements the maximum value comparison.
Class math{
Public $pi = 3.14;

Public Function Max ($num 1, $num 2) {
echo self:: $PI; There seems to be no problem with the call here.
Return $num 1 > $num 2? $num 1: $num 2;
}
public static function Max3 ($num 1, $num 2, $num 3) {
$num 1 = Self::max ($num 1, $num 2);
$num 2 = Self::max ($num 2, $num 3);
$num 1 = Self::max ($num 1, $num 2);
return $num 1;
}
}
$a = 99;
$b = 77;
$c = 188;
echo "Displays the maximum value in the $a $b $c is";
echo "
";
Echo math::max3 ($a, $b, $c);
?>

Program Run Result:
1
2

Display the maximum value in 99 77 188 is
Fatal error:access to undeclared static Property:math:: $pi in e:phpprojectstest.php on line 7

For more detailed information, please see: http://www.bKjia.c0m/phper/php/56640.htm

http://www.bkjia.com/PHPjc/667913.html www.bkjia.com true http://www.bkjia.com/PHPjc/667913.html techarticle The static keyword declares that a property or method is related to a class and not to a particular instance of a class, so such a property or method is also known as a class property or a class method. If you visit ...

  • Related Article

    E-Commerce Solutions

    Leverage the same tools powering the Alibaba Ecosystem

    Learn more >

    Apsara Conference 2019

    The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

    Learn more >

    Alibaba Cloud Free Trial

    Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

    Learn more >

    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.