Home > php教程 > PHP开发 > PHP object-oriented journey: in-depth understanding of static variables and methods

PHP object-oriented journey: in-depth understanding of static variables and methods

黄舟
Release: 2016-12-12 17:06:07
Original
1405 people have browsed it

The static keyword declares that an attribute or method is related to the class, not to a specific instance of the class. Therefore, this type of attribute or method is also called "class attribute" or "class method".

If access control permissions allow, you can call it directly using the class name plus two colons "::" without creating an object of this class.

static keyword can be used to modify variables and methods.

You can directly access the static attributes and static methods in the class without instantiation.

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

static properties have only one copy in memory and are shared by all instances.

Use the self:: keyword to access static members of the current class.
Static properties public features

All instances of a class share the static properties in the class.

In other words, even if there are multiple instances in the memory, there is only one copy of the static attributes.

In the following example, a counter $count attribute is set to private and static Grooming. In this way, the outside world cannot directly access the $count property. As a result of the program running, we also see that multiple instances are using the same static $count attribute.

<?
class user{
    private static $count = 0 ; //记录所有用户的登录情况.
    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";
?>
Copy after login

Program running result:
1
2

now here have 3 users
now here have 2 users jb51.net
Static properties are called directly

Static properties can be used directly without instantiation, and can be used directly before the class is created.

The method used is class name::static property name.

<?
class Math{
    public static $pi = 3.14;

}
//求一个半径3的园的面积。
$r = 3;
echo "半径是 $r 的面积是<br>";
echo Math::$pi * $r * $r ;

echo "<br><br>";
//这里我觉得 3.14 不够精确,我把它设置的更精确。
Math::$pi = 3.141592653589793;
echo "半径是 $r 的面积是<br>";
echo Math::$pi * $r * $r ;
?>
Copy after login

Program running result:
1
2
3
4

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

The class is not created, and the static attributes can be used directly. When are static properties created in memory? I haven't seen any relevant information in PHP. Citing concepts in Java to explain should also be universal.

Static properties and methods, created when the class is called. When a class is called, it means that the class is created or any static member in the class is called.
Static methods

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

The method used is class name:: static method name.

Let’s continue writing this Math class to perform mathematical calculations. We design a method to calculate the maximum value. Since it is a mathematical operation, we do not need to instantiate this class. It would be much more convenient if this method can be taken and used.

We designed this class just to demonstrate the static method. PHP provides the max() function to compare numerical values.

<?
class Math{

    public static function Max($num1,$num2){
        return $num1 > $num2 ? $num1 : $num2;
    }    
}
$a = 99;
$b = 88;
echo "显示 $ a 和 $ b 中的最大值是";
echo "<br>";
echo Math::Max($a,$b);
echo "<br>";echo "<br>";echo "<br>";
$a = 99;
$b = 100;
echo "显示 $ a 和 $ b 中的最大值是";
echo "<br>";
echo Math::Max($a,$b);
?>
Copy after login

Program running result:

Show $ a and $ b The maximum value is
99
Show $ a and $ b The maximum value is
100
How to call a static method from a static method

The first example, when a static method calls other static methods, use the class name directly.

<?
// 实现最大值比较的Math类。
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 "显示 $a  $b $c  中的最大值是";
echo "<br>";
echo Math::Max3($a,$b,$c);
?>
Copy after login

Program running result:
1
2

shows that the maximum value among 99 77 88 is
99

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

<?
// 实现最大值比较的Math类。
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 "显示 $a  $b $c  中的最大值是";
echo "<br>";
echo Math::Max3($a,$b,$c);
?>
Copy after login

Program running result:
1
2

Displays that the maximum value among 99 77 88 is
99
Static method calls static properties

Use class name::static property name to call static properties in this class .

<?
//
class Circle{
    public static $pi = 3.14;

    public static function circleAcreage($r){
      return $r * $r * Circle::$pi;
    }
}
$r = 3;
echo " 半径 $r 的圆的面积是 " . Circle::circleAcreage($r);
?>
Copy after login

Program running result:
1

The area of ​​a circle with radius 3 is 28.26

Use self:: to call the static properties of this class. (Suggestion)

<?
//
class Circle{
    public static $pi = 3.14;

    public static function circleAcreage($r){
      return $r * $r * self::$pi;
    }
}
$r = 3;
echo " 半径 $r 的圆的面积是 " . Circle::circleAcreage($r);
?>
Copy after login

Program running result:
1

The area of ​​a circle with radius 3 is 28.26
Static methods cannot call non-static properties

Static methods cannot call non-static properties. Non-static properties cannot be called using self::.

<?
//
class Circle{
    public $pi = 3.14;

    public static function circleAcreage($r){
      return $r * $r * self::pi;
    }
}
$r = 3;
echo " 半径 $r 的圆的面积是 " . Circle::circleAcreage($r);
?>
Copy after login

Program running 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 non-static properties.

<?
//
class Circle{
    public $pi = 3.14;

    public static function circleAcreage($r){
      return $r * $r * $this->pi;
    }
}
$r = 3;
echo " 半径 $r 的圆的面积是 " . Circle::circleAcreage($r);
?>
Copy after login

Program execution result:
1

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

In PHP5, the $this identifier cannot be used in static methods to call non-static methods.

<?
// 实现最大值比较的Math类。
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 "显示 $a  $b $c  中的最大值是";
echo "<br>";
echo Math::Max3($a,$b,$c);
?>
Copy after login

The program running result:

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

When a non-static method in a class is called by self::, the system will automatically convert this method into a static method.

The following code was executed and produced results. Because the Max method is converted into a static method by the system.

<?
// 实现最大值比较的Math类。
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 "显示 $a  $b $c  中的最大值是";
echo "<br>";
echo Math::Max3($a,$b,$c);
?>
Copy after login

The program running result:
1
2

shows that the maximum value among 99 77 188 is
188

In the following example, we let the static method Max3 use self:: to call the non-static method Max, let The non-static method Max calls the non-static property $pi through $this.

An error was reported when running. This error is the same as the previous example 3-1-9.php. This time, the non-static method Max reported an error of calling non-static properties by a static method.

This proves something. The non-static method Max we defined here is automatically converted into a static method by the system.

<?
// 实现最大值比较的Math类。
class Math{
    public $pi = 3.14;

    public function Max($num1,$num2){
        echo self::$pi;  //这里的调用看来不应该有问题.
        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 "显示 $a  $b $c  中的最大值是";
echo "<br>";
echo Math::Max3($a,$b,$c);
?>
Copy after login

程序运行结果:
1
2

显示 99 77 188 中的最大值是
Fatal error: Access to undeclared static property: Math::$pi in E:PHPProjectstest.php on line 7


Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template