php static The "static" in static methods means that these properties and methods can be called directly without instantiating the class; static is a keyword used to modify the properties and methods of the class. Its usage syntax is such as "class Foo {public static $my_static = 'hello';}".
The operating environment of this tutorial: Windows 7 system, PHP version 8.1, Dell G3 computer.
PHP static static detailed explanation
PHP class attributes and methods need to be called after the class is instantiated (except constant attributes), however, PHP also provides static attributes and static methods. The so-called "static" means that these properties and methods can be called directly without instantiating the class. It’s not that static classes cannot be instantiated, but they can be used without instantiation.
Use the static keyword to modify the attributes and methods of the class, and call these attributes and methods static attributes and static methods.
Syntax:
static 属性名
Example:
<?php class Foo { public static $my_static = 'hello'; } ?>
Syntax:
static function 方法名{ //代码 }
Example:
<?php class Foo { public static function staticValue() { return 'hello'; } } ?>
Note: Static properties and methods are the same as object properties and methods. They support setting private
, protected
, public
Three visibility levels.
Pass class name::property/method
# Called in ## mode.
<?php class Mystatic { public static $staticvalue = 'zhangsan'; public static function staticMethod() { $a = 'hello'; return $a; } } echo '$staticvalue: '.Mystatic::$staticvalue.PHP_EOL; echo '$a: '.Mystatic::staticMethod().PHP_EOL; ?>
PHP_EOL represents the system newline character.
$staticvalue: zhangsan $a: hello
Object name::Property/Method
.
<?php class Mystatic { public static $staticvalue = 'zhangsan'; public static function staticMethod() { $a = 'hello'; return $a; } } $obj = new Mystatic(); echo '$staticvalue: '.$obj::$staticvalue.PHP_EOL; echo '$a: '.$obj::staticMethod(); ?>
$staticvalue: zhangsan $a: hello
object name-> method, object name-> attribute
will fail.
<?php error_reporting(0); class Mystatic { public static $staticvalue = 'zhangsan'; public static function staticMethod() { $a = 'hello'; return $a; } } $obj = new Mystatic(); echo '$staticvalue: '.$obj -> staticvalue.PHP_EOL; echo '$a: '.$obj -> staticMethod(); ?>
$staticvalue: $a: hello
self::properties/methods
is called, self points to the current class, just like $this points to the current object; and in the absence of instantiation, $this The pointer points to an empty object
, so it cannot be touched to reference static properties and methods.
<?php class Mystatic { public static $staticvalue = 'zhangsan'; public static function staticMethod() { $a = 'hello'; return $a; } public function noStatic(){ echo '$staticvalue: '.self::$staticvalue.PHP_EOL; echo '$a: '.self::staticMethod(); } } $obj = new Mystatic(); $obj -> noStatic(); ?>
$staticvalue: zhangsan $a: hello
<?php class Mystatic { public static $staticvalue = 'zhangsan'; public static function staticMethod1() { $a = 'hello'; return $a; } public static function staticMethod2(){ echo '$staticvalue: '.self::$staticvalue.PHP_EOL; echo '$a: '.self::staticMethod1().PHP_EOL; } } Mystatic::staticMethod2(); $obj = new Mystatic(); $obj -> staticMethod2(); ?>
$staticvalue: zhangsan $a: hello $staticvalue: zhangsan $a: hello
Full class name:: for reference.
<?php class Mystatic1 { public static $staticvalue1 = 'xiaomin'; } class Mystatic2 { public static $staticvalue2 = 'zhangsan'; public static function staticMethod() { echo '$staticvalue1: '.Mystatic1::$staticvalue1.PHP_EOL; echo '$staticvalue2: '.self::$staticvalue2.PHP_EOL; } } Mystatic2::staticMethod(); $obj = new Mystatic2(); $obj -> staticMethod(); ?>
$staticvalue1: xiaomin $staticvalue2: zhangsan $staticvalue1: xiaomin $staticvalue2: zhangsan
,
protected static properties/methods at visibility level
and protected
attributes are restricted to being called within the class. If you want to call it outside the class, you need to provide a public
method for the outside. The method accesses private
, protected
attributes. Terminology: A class provides an interface to the outside world. <div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:php;toolbar:false"><?php
class Mystatic {
public static $staticvalue1 = &#39;zhangsan&#39;;
private static $staticvalue2 = 20;
protected static $staticvalue3 = &#39;student&#39;;
private static function staticMethod() {
$a = &#39;hello&#39;;
return $a;
}
public function port1() {
echo &#39;$staticvalue1: &#39;.self::$staticvalue1.PHP_EOL;
echo &#39;$staticvalue2: &#39;.self::$staticvalue2.PHP_EOL;
echo &#39;$staticvalue3: &#39;.self::$staticvalue3.PHP_EOL;
echo &#39;$a: &#39;.self::staticMethod().PHP_EOL;
}
public static function port2() {
echo &#39;$staticvalue1: &#39;.self::$staticvalue1.PHP_EOL;
echo &#39;$staticvalue2: &#39;.self::$staticvalue2.PHP_EOL;
echo &#39;$staticvalue3: &#39;.self::$staticvalue3.PHP_EOL;
echo &#39;$a: &#39;.self::staticMethod().PHP_EOL;
}
}
$obj = new Mystatic();
$obj -> port1();
echo "\r\n";
Mystatic::port2();
?></pre><div class="contentsignin">Copy after login</div></div>
Result:
$staticvalue1: zhangsan $staticvalue2: 20 $staticvalue3: student $a: hello $staticvalue1: zhangsan $staticvalue2: 20 $staticvalue3: student $a: hello
Static properties support dynamic modification
. Static properties need to add $, and there is no $ before the constant name, so there is no need to add it when accessing class constants. 1. Class constants
<?php class Myconst { const A = 1234; } $obj1 = new Myconst(); echo 'A: '.$obj1::A.PHP_EOL; $obj1->A='aaa'; //$obj1::A='aaa';会报错 echo "\r\n"; $obj2 = new Myconst(); echo 'A: '.$obj2::A.PHP_EOL; ?>
A: 1234 A: 1234
2. Static attributes
<?php class Mystatic { public static $A = 1234; } echo '$A: '.Mystatic::$A.PHP_EOL; Mystatic::$A = 6666; echo '$A: '.Mystatic::$A.PHP_EOL; $obj1 = new Mystatic(); echo '$A: '.$obj1::$A.PHP_EOL; Mystatic::$A = 5555; $obj2 = new Mystatic(); echo '$A: '.$obj2::$A.PHP_EOL; echo '$A: '.$obj1::$A.PHP_EOL; ?>
$A: 1234 $A: 6666 $A: 6666 $A: 5555 $A: 5555
Inheritance of static members Like overriding
1. Static attributes
<?php class Mystatic { static public $a; //定义一个静态变量 static function test() //定义静态方法来操作并输出静态变量 { self::$a++; return self::$a; } } class Mystatic2 extends Mystatic //定义一个子类 { static function test() //定义子类的静态方法 { self::$a++; //访问并操作父类的静态变量 return self::$a; } } $obj1=new Mystatic; //新建父类对象 echo '此时$a的值为: '.$obj1->test().PHP_EOL; //通过对象调用静态方法test,静态属性$a的值+1 $obj2=new Mystatic; //新建另一个父类对象 echo '此时$a的值为: '.$obj2->test().PHP_EOL; //新父类对象调用静态方法test,静态属性$a的值+1+1 $obj3=new Mystatic2; //新建子类对象 echo '此时$a的值为: '.$obj3->test().PHP_EOL; //子类对象调用同名静态方法test, 静态属性$a的值+1+1+1 echo Mystatic::$a.PHP_EOL; //通过父类::直接访问静态成员$a变量 echo $obj1::$a.PHP_EOL; //通过对象名::可以直接访问静态成员$a变量 ?>
Result:
此时$a的值为: 1 此时$a的值为: 2 此时$a的值为: 3 3 3
2. Static method
<?php class Mystatic1 { public static function getclassName() { return __CLASS__; } public static function whoclassName() { echo self::getclassName().PHP_EOL; } } class Mystatic2 extends Mystatic1{ public static function getclassName() { return __CLASS__; } } echo Mystatic1::getclassName().PHP_EOL; echo Mystatic2::getclassName().PHP_EOL; ?>
The class name of the current class can be obtained through
__CLASS__. We call the getClassName
method of the two classes respectively: Result:
Mystatic1 Mystatic2
indicates that the subclass overrides the static method of the same name of the parent class. Similarly, we can also call the
whoclassName method in the parent class on the subclass: <div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:php;toolbar:false"><?php
class Mystatic1 {
public static function getclassName() {
return __CLASS__;
}
public static function whoclassName() {
echo self::getclassName().PHP_EOL;
}
}
class Mystatic2 extends Mystatic1{
public static function getclassName() {
return __CLASS__;
}
}
echo Mystatic1::whoclassName();
echo Mystatic2::whoclassName();
?></pre><div class="contentsignin">Copy after login</div></div>
Result:
Mystatic1 Mystatic1
Why is the second printed result the parent class name
Mystatic1 instead of the subclass name Mystatic2
? This is because the $this
pointer always points to the reference object that holds it, while self
points to the class that holds it when it is defined instead of
Class when calling, in order to solve this problem, starting from PHP 5.3, a new feature called
Delayed static binding has been added.
延迟静态绑定(Late Static Bindings)针对的是静态方法的调用,使用该特性时不再通过 <span style="background-color:#fe2c24;">self::</span>
引用静态方法,而是通过 static::
,如果是在定义它的类中调用,则指向当前类
,此时和 self
功能一样,如果是在子类或者其他类中调用,则指向调用该方法所在的类
。
<?php class Mystatic1 { public static function getclassName() { return __CLASS__; } public static function whoclassName() { echo static::getclassName().PHP_EOL; } } class Mystatic2 extends Mystatic1{ //self改为static public static function getclassName() { return __CLASS__; } } echo Mystatic1::whoclassName(); echo Mystatic2::whoclassName(); ?>
结果:
Mystatic1 Mystatic2
表明后期静态绑定生效,即 static
指向的是调用它的方法所在的类,而不是定义时,所以称之为延迟静态绑定。
此外,还可以通过 static::class
来指向当前调用类的类名,例如我们可以通过它来替代 __CLASS__
,这样上述子类就没有必要重写 getClassName
方法了:
<?php class Mystatic1 { public static function getclassName() { return static::class; } public static function whoclassName() { echo static::getclassName().PHP_EOL; } } class Mystatic2 extends Mystatic1{} echo Mystatic1::getclassName().PHP_EOL; echo Mystatic2::getclassName().PHP_EOL; echo Mystatic1::whoclassName(); echo Mystatic2::whoclassName(); ?>
结果:
Mystatic1 Mystatic2 Mystatic1 Mystatic2
同理,self::class
则始终指向的是定义它的类。
静态属性和方法可以直接通过类引用,所以又被称作类属性和类方法。非静态属性和非静态方法需要实例化后通过对象引用,因此被称作对象属性和对象方法。
静态属性保存在类空间,非静态属性保存在对象空间。非静态方法可以访问类中的任何成员(包括静态),静态方法只能访问类中的静态成员。
静态方法可以直接调用,类名调用和对象调用(类名或self::
调用),但是非静态方法只能通过对象调用(对象名或$this->
调用)。
一个类的所有实例对象,共用类中的静态属性。如果修改了这个类静态属性,那么这个类的所有对象都能访问到这个新值。
静态方法和属性的生命周期跟相应的类一样长,静态方法和静态属性会随着类的定义而被分配和装载入内存中。一直到线程结束,静态属性和方法才会被销毁。 非静态方法和属性的生命周期和类的实例化对象一样长,只有当类实例化了一个对象,非静态方法和属性才会被创建,而当这个对象被销毁时,非静态方法也马上被销毁。静态方法和静态变量创建后始终使用同一块内存,而使用实例的方式会创建多个内存。但静态方法效率上要比实例化高,静态方法的缺点是不自动进行销毁,而实例化的则可以做销毁。
静态方法最适合工具类中方法的定义;比如文件操作,日期处理方法等.
静态变量适合全局变量的定义.
推荐学习:《PHP视频教程》
The above is the detailed content of What is the static method of php. For more information, please follow other related articles on the PHP Chinese website!