Home > Backend Development > PHP Tutorial > Summary of examples of how to use static variables in php

Summary of examples of how to use static variables in php

伊谢尔伦
Release: 2023-03-12 07:08:02
Original
1540 people have browsed it

static keywordDeclares that a attribute or method is related to the class, not to a specific instance of the class, Therefore, such properties or methods are also called "class properties" or "class methods".

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

The 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's properties and methods can only access static properties and methods, and cannot access non-static properties and methods. 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.
Public properties of static properties

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, and private and static modifications are set. 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.

<?php
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 user
now here have 2 user jb51.net
Static attributes 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.

<?php
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 a radius of 3 is
28.26
The area with a radius of 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 are 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 method

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

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

Let’s continue to write 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.

<?php
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:

Displays that the maximum value of $a and $b is
99
Displays that the maximum value of $a and $b is
100
How to call static methods from static methods

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

<?php
// 实现最大值比较的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

The maximum value displayed among 99 77 88 is
99

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

<?php
// 实现最大值比较的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 property

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

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

<?php
//
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 attributes

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

<?php
//
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

Cannot use $this either Get the value of a non-static property.

<?php
//
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 running 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.

<?php
// 实现最大值比较的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

Program running result:

Displays 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.

<?php
// 实现最大值比较的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

程序运行结果:
1
2

显示 99 77 188 中的最大值是
188

下面的例子中,我们让静态方法Max3 用过self::调用了非静态方法Max,有让非静态方法Max通过$this 调用非静态属性 $pi 。

在运行是报出了错误,这个错误和前一个例子 3-1-9.php一样,这次倒是非静态方法Max报出了静态方法调用非静态属性的错误。

这倒是证明了一点,在这里我们定义的非静态方法 Max 被系统自动转换成静态方法了。

<?php
// 实现最大值比较的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

The above is the detailed content of Summary of examples of how to use static variables in php. For more information, please follow other related articles on the PHP Chinese website!

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 Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template