Detailed analysis of the use and differences between static methods (static) and non-static methods in PHP

黄舟
Release: 2023-03-12 12:06:02
Original
1428 people have browsed it

static keyword is used to modify attributes and methods. These attributes and methods are called static attributes and static methods.

The static keyword declares that a property or method is related to the class, rather than to a specific instance of the class. Therefore, this type of property or method is also called "class" Attribute" or "Class Method"

If Access Control permissions allow, you can directly use the class name plus two colons "::" without creating an object of this class. transfer.

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

You can directly access static properties and static methods in a class without instantiation.

static's properties and methods can only access static properties and methods, but cannot access non-static properties and methods. Because when static properties and methods are created, there may not 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.

All instances of a class share 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 attribute. 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

Static properties are directly called
Static properties can be used directly without instantiation. The class can be used directly before it 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


类没有创建,静态属性就可以直接使用。那静态属性在什么时候在内存中被创建? 在PHP中没有看到相关的资料。引用Java中的概念,来解释应该也具有通用性

。静态属性和方法,在类被调用时创建。

静态方法
静态方法不需要所在类被实例化就可以直接使用。

使用的方式是类名::静态方法名

下面我们继续写这个Math类,用来进行数学计算。我们设计一个方法用来算出其中的最大值。既然是数学运算,我们也没有必要去实例化这个类,如果这个方法

可以拿过来就用就方便多了。我们这只是为了演示static方法而设计的这个类。在PHP提供了 max() 函数比较数值。

view plaincopy to clipboardprint?
<?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



静态方法如何调用静态方法
第一个例子,一个静态方法调用其它静态方法时,使用self::

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


静态方法调用静态属性
使用self:: 调用本类的静态属性。

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




静态方法不能调用非静态属性 。不能使用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


也不能使用 $this 获取非静态属性的值。

静态方法调用非静态方法
PHP5中,在静态方法中不能使用 $this 标识调用非静态方法。

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



当一个类中有非静态方法被self:: 调用时,系统会自动将这个方法转换为静态方法。

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


The above is the detailed content of Detailed analysis of the use and differences between static methods (static) and non-static methods 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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!