PHP面向对象之旅:深入理解static变量与方法_PHP
static关键字声明一个属性或方法是和类相关的,而不是和类的某个特定的实例相关,因此,这类属性或方法也称为“类属性”或“类方法”。
如果访问控制权限允许,可不必创建该类对象而直接使用类名加两个冒号“::”调用。
static关键字可以用来修饰变量、方法。
不经过实例化,就可以直接访问类中static的属性和static的方法。
static 的属性和方法,只能访问static的属性和方法,不能类访问非静态的属性和方法。因为静态属性和方法被创建时,可能还没有任何这个类的实例可以被调用。
static的属性,在内存中只有一份,为所有的实例共用。
使用self:: 关键字访问当前类的静态成员。
静态属性公用特性
一个类的所有实例,共用类中的静态属性。
也就是说,在内存中即使有多个实例,静态的属性也只有一份。
下面例子中的设置了一个计数器$count属性,设置private 和 static 修饰。这样,外界并不能直接访问$count属性。而程序运行的结果我们也看到多个实例在使用同一个静态的$count 属性。
复制代码 代码如下:
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 "
";
unset( $user3);
echo "now here have ".$user1->getCount()." user";
?>
程序运行结果:
1
2
now here have 3 user
now here have 2 user bitsCN.com
静态属性直接调用
静态属性不需要实例化就可以直接使用,在类还没有创建时就可以直接使用。
使用的方式是 类名::静态属性名。
复制代码 代码如下:
class Math{
public static $pi = 3.14;
}
//求一个半径3的园的面积。
$r = 3;
echo "半径是 $r 的面积是
";
echo Math::$pi * $r * $r ;
echo "
";
//这里我觉得 3.14 不够精确,我把它设置的更精确。
Math::$pi = 3.141592653589793;
echo "半径是 $r 的面积是
";
echo Math::$pi * $r * $r ;
?>
程序运行结果:
1
2
3
4
半径是 3 的面积是
28.26
半径是 3 的面积是
28.2743338823
类没有创建,静态属性就可以直接使用。那静态属性在什么时候在内存中被创建?在PHP中没有看到相关的资料。引用Java中的概念,来解释应该也具有通用性。
静态属性和方法,在类被调用时创建。类被调用,是指类被创建或者类中的任何静态成员被调用。
静态方法
静态方法不需要所在类被实例化就可以直接使用。
使用的方式是 类名::静态方法名。
下面我们继续写这个Math类,用来进行数学计算。我们设计一个方法用来算出其中的最大值。既然是数学运算,我们也没有必要去实例化这个类,如果这个方法可以拿过来就用就方便多了。
我们这只是为了演示static方法而设计的这个类。在PHP提供了 max() 函数比较数值。
复制代码 代码如下:
class Math{
public static function Max($num1,$num2){
return $num1 > $num2 ? $num1 : $num2;
}
}
$a = 99;
$b = 88;
echo "显示 $ a 和 $ b 中的最大值是";
echo "
";
echo Math::Max($a,$b);
echo "
";echo "
";echo "
";
$a = 99;
$b = 100;
echo "显示 $ a 和 $ b 中的最大值是";
echo "
";
echo Math::Max($a,$b);
?>
程序运行结果:
显示 $ a 和 $ b 中的最大值是
99
显示 $ a 和 $ b 中的最大值是
100
静态方法如何调用静态方法
第一个例子,一个静态方法调用其它静态方法时,直接使用 类名。
复制代码 代码如下:
// 实现最大值比较的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 "
";
echo Math::Max3($a,$b,$c);
?>
程序运行结果:
1
2
显示 99 77 88 中的最大值是
99
也可以使用self:: 调用当前类中的其它静态方法。(建议)
复制代码 代码如下:
// 实现最大值比较的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 "
";
echo Math::Max3($a,$b,$c);
?>
程序运行结果:
1
2
显示 99 77 88 中的最大值是
99
静态方法调用静态属性
使用 类名::静态属性名 调用本类中的静态属性。
复制代码 代码如下:
//
class Circle{
public static $pi = 3.14;
public static function circleAcreage($r){
return $r * $r * Circle::$pi;
}
}
$r = 3;
echo " 半径 $r 的圆的面积是 " . Circle::circleAcreage($r);
?>
程序运行结果:
1
半径 3 的圆的面积是 28.26
使用self:: 调用本类的静态属性。(建议)
复制代码 代码如下:
//
class Circle{
public static $pi = 3.14;
public static function circleAcreage($r){
return $r * $r * self::$pi;
}
}
$r = 3;
echo " 半径 $r 的圆的面积是 " . Circle::circleAcreage($r);
?>
程序运行结果:
1
半径 3 的圆的面积是 28.26
静态方法不能调用非静态属性
静态方法不能调用非静态的属性。不能使用self::调用非静态属性。
复制代码 代码如下:
//
class Circle{
public $pi = 3.14;
public static function circleAcreage($r){
return $r * $r * self::pi;
}
}
$r = 3;
echo " 半径 $r 的圆的面积是 " . Circle::circleAcreage($r);
?>
程序运行结果:
1
Fatal error: Undefined class constant 'pi' in E:PHPProjectstest.php on line 7
也不能使用 $this 获取非静态属性的值。
复制代码 代码如下:
//
class Circle{
public $pi = 3.14;
public static function circleAcreage($r){
return $r * $r * $this->pi;
}
}
$r = 3;
echo " 半径 $r 的圆的面积是 " . Circle::circleAcreage($r);
?>
程序运行结果:
1
Fatal error: Using $this when not in object context in E:PHPProjectstest.php on line 7
静态方法调用非静态方法
PHP5中,在静态方法中不能使用 $this 标识调用非静态方法。
复制代码 代码如下:
// 实现最大值比较的Math类。
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 "显示 $a $b $c 中的最大值是";
echo "
";
echo Math::Max3($a,$b,$c);
?>
程序运行结果:
显示 99 77 188 中的最大值是
Fatal error: Using $this when not in object context in E:test.php on line 10
当一个类中有非静态方法被 self:: 调用时,系统会自动将这个方法转换为静态方法。
下面的代码被执行了,而且有结果。因为Max方法被系统转换为静态方法了。
复制代码 代码如下:
// 实现最大值比较的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 "
";
echo Math::Max3($a,$b,$c);
?>
程序运行结果:
1
2
显示 99 77 188 中的最大值是
188
下面的例子中,我们让静态方法Max3 用过self::调用了非静态方法Max,有让非静态方法Max通过$this 调用非静态属性 $pi 。
在运行是报出了错误,这个错误和前一个例子 3-1-9.php一样,这次倒是非静态方法Max报出了静态方法调用非静态属性的错误。
这倒是证明了一点,在这里我们定义的非静态方法 Max 被系统自动转换成静态方法了。
复制代码 代码如下:
// 实现最大值比较的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 "
";
echo Math::Max3($a,$b,$c);
?>
程序运行结果:
1
2
显示 99 77 188 中的最大值是
Fatal error: Access to undeclared static property: Math::$pi in E:PHPProjectstest.php on line 7

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Alipay PHP...

Session hijacking can be achieved through the following steps: 1. Obtain the session ID, 2. Use the session ID, 3. Keep the session active. The methods to prevent session hijacking in PHP include: 1. Use the session_regenerate_id() function to regenerate the session ID, 2. Store session data through the database, 3. Ensure that all session data is transmitted through HTTPS.

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

The application of SOLID principle in PHP development includes: 1. Single responsibility principle (SRP): Each class is responsible for only one function. 2. Open and close principle (OCP): Changes are achieved through extension rather than modification. 3. Lisch's Substitution Principle (LSP): Subclasses can replace base classes without affecting program accuracy. 4. Interface isolation principle (ISP): Use fine-grained interfaces to avoid dependencies and unused methods. 5. Dependency inversion principle (DIP): High and low-level modules rely on abstraction and are implemented through dependency injection.

How to debug CLI mode in PHPStorm? When developing with PHPStorm, sometimes we need to debug PHP in command line interface (CLI) mode...

How to automatically set the permissions of unixsocket after the system restarts. Every time the system restarts, we need to execute the following command to modify the permissions of unixsocket: sudo...

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

Sending JSON data using PHP's cURL library In PHP development, it is often necessary to interact with external APIs. One of the common ways is to use cURL library to send POST�...
