What is the static method of php

藏色散人
Release: 2023-03-17 10:30:01
Original
1932 people have browsed it

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';}".

What is the static method of php

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.

Definition of static members

Use the static keyword to modify the attributes and methods of the class, and call these attributes and methods static attributes and static methods.

1. Static attribute

Syntax:

static 属性名
Copy after login

Example:

<?php
class Foo {
  public static $my_static = &#39;hello&#39;;  
}
?>
Copy after login

2. Static method

Syntax:

static function 方法名{
    //代码
}
Copy after login

Example:

<?php
class Foo {
  public static function staticValue() {
     return &#39;hello&#39;;
  }
}
?>
Copy after login

Note: Static properties and methods are the same as object properties and methods. They support setting private, protected, public Three visibility levels.

Call of static members

1. Call static properties/methods outside the class

Pass class name::property/method # Called in ## mode.

<?php
class Mystatic {
  public static $staticvalue = &#39;zhangsan&#39;;
  public static function staticMethod() {
    $a = &#39;hello&#39;;
    return $a;
  }
}
echo &#39;$staticvalue: &#39;.Mystatic::$staticvalue.PHP_EOL;
echo &#39;$a: &#39;.Mystatic::staticMethod().PHP_EOL;
?>
Copy after login
Note: The predefined constant

PHP_EOL represents the system newline character.

Result:

$staticvalue: zhangsan
$a: hello
Copy after login
Copy after login
Copy after login
Called by

Object name::Property/Method .

<?php
class Mystatic {
  public static $staticvalue = &#39;zhangsan&#39;;
  public static function staticMethod() {
    $a = &#39;hello&#39;;
    return $a;
  }
}
$obj = new Mystatic();
echo &#39;$staticvalue: &#39;.$obj::$staticvalue.PHP_EOL;
echo &#39;$a: &#39;.$obj::staticMethod();
?>
Copy after login
Result:

$staticvalue: zhangsan
$a: hello
Copy after login
Copy after login
Copy after login
Called by

object name-> method, object name-> attribute will fail.

<?php
error_reporting(0);
class Mystatic {
  public static $staticvalue = &#39;zhangsan&#39;;
  public static function staticMethod() {
    $a = &#39;hello&#39;;
    return $a;
  }
}
$obj = new Mystatic();
echo &#39;$staticvalue: &#39;.$obj -> staticvalue.PHP_EOL;
echo '$a: '.$obj -> staticMethod();
?>
Copy after login
Result:

$staticvalue:
$a: hello
Copy after login
2. Call static properties/methods in non-static methods

Pass

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 = &#39;zhangsan&#39;;
  public static function staticMethod() {
    $a = &#39;hello&#39;;
    return $a;
  }
  public function noStatic(){
    echo &#39;$staticvalue: &#39;.self::$staticvalue.PHP_EOL;
    echo &#39;$a: &#39;.self::staticMethod();

  }
}
$obj = new Mystatic();
$obj -> noStatic();
?>
Copy after login
Result:

$staticvalue: zhangsan
$a: hello
Copy after login
Copy after login
Copy after login
3. Calling static properties/methods in static methods

is the same as calling static properties/methods in non-static methods.

<?php
class Mystatic {
  public static $staticvalue = &#39;zhangsan&#39;;
  public static function staticMethod1() {
    $a = &#39;hello&#39;;
    return $a;
  }
  public static function staticMethod2(){
    echo &#39;$staticvalue: &#39;.self::$staticvalue.PHP_EOL;
    echo &#39;$a: &#39;.self::staticMethod1().PHP_EOL;

  }
}
Mystatic::staticMethod2();
$obj = new Mystatic();
$obj -> staticMethod2();
?>
Copy after login
Result:

$staticvalue: zhangsan
$a: hello
$staticvalue: zhangsan
$a: hello
Copy after login
4. Call the static properties/methods of another class

If you call the static properties and methods of other classes in one class, you need to pass

Full class name:: for reference.

<?php
class Mystatic1 {
  public static $staticvalue1 = &#39;xiaomin&#39;;
}
class Mystatic2 {
  public static $staticvalue2 = &#39;zhangsan&#39;;
  public static function staticMethod() {
    echo &#39;$staticvalue1: &#39;.Mystatic1::$staticvalue1.PHP_EOL;
    echo &#39;$staticvalue2: &#39;.self::$staticvalue2.PHP_EOL;

  }
}
Mystatic2::staticMethod();
$obj = new Mystatic2();
$obj -> staticMethod();
?>
Copy after login
Result:

$staticvalue1: xiaomin
$staticvalue2: zhangsan
$staticvalue1: xiaomin
$staticvalue2: zhangsan
Copy after login
5. Call

private, protected static properties/methods at visibility level

Due to# The ##private

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">&lt;?php class Mystatic { public static $staticvalue1 = &amp;#39;zhangsan&amp;#39;; private static $staticvalue2 = 20; protected static $staticvalue3 = &amp;#39;student&amp;#39;; private static function staticMethod() { $a = &amp;#39;hello&amp;#39;; return $a; } public function port1() { echo &amp;#39;$staticvalue1: &amp;#39;.self::$staticvalue1.PHP_EOL; echo &amp;#39;$staticvalue2: &amp;#39;.self::$staticvalue2.PHP_EOL; echo &amp;#39;$staticvalue3: &amp;#39;.self::$staticvalue3.PHP_EOL; echo &amp;#39;$a: &amp;#39;.self::staticMethod().PHP_EOL; } public static function port2() { echo &amp;#39;$staticvalue1: &amp;#39;.self::$staticvalue1.PHP_EOL; echo &amp;#39;$staticvalue2: &amp;#39;.self::$staticvalue2.PHP_EOL; echo &amp;#39;$staticvalue3: &amp;#39;.self::$staticvalue3.PHP_EOL; echo &amp;#39;$a: &amp;#39;.self::staticMethod().PHP_EOL; } } $obj = new Mystatic(); $obj -&gt; port1(); echo &quot;\r\n&quot;; Mystatic::port2(); ?&gt;</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
Copy after login

Static properties support dynamic modification

In actual applications, there will be multiple objects of a class, which may share a copy of data. Both class constants and static properties can be implemented. Static properties are similar (identical) to class constants, the only difference is that class constants cannot be changed, while static properties can be changed. The access method is the same and can be accessed using

::

. 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 &#39;A: &#39;.$obj1::A.PHP_EOL;
$obj1->A='aaa';
//$obj1::A='aaa';会报错
echo "\r\n";
$obj2 = new Myconst();
echo 'A: '.$obj2::A.PHP_EOL;
?>
Copy after login

Result:

A: 1234

A: 1234
Copy after login

2. Static attributes

<?php
class Mystatic {
  public static $A = 1234;
}
echo &#39;$A: &#39;.Mystatic::$A.PHP_EOL;
Mystatic::$A = 6666;
echo &#39;$A: &#39;.Mystatic::$A.PHP_EOL;
$obj1 = new Mystatic();
echo &#39;$A: &#39;.$obj1::$A.PHP_EOL;
Mystatic::$A = 5555;
$obj2 = new Mystatic();
echo &#39;$A: &#39;.$obj2::$A.PHP_EOL;
echo &#39;$A: &#39;.$obj1::$A.PHP_EOL;
?>
Copy after login

Result:

$A: 1234
$A: 6666
$A: 6666
$A: 5555
$A: 5555
Copy after login

Inheritance of static members Like overriding

and non-static properties/methods, static properties and methods can also be inherited by subclasses, and static properties and methods can also be overridden by subclasses.

1. Static attributes

Subclasses can override the static member variables of the parent class, but the static variables of the parent class still exist. These two static member variables are independent. They will be called according to the call The class names are accessed separately.

<?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 &#39;此时$a的值为: &#39;.$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变量
?>
Copy after login

Result:

此时$a的值为: 1
此时$a的值为: 2
此时$a的值为: 3
3
3
Copy after login

2. Static method

Subclasses can override the static methods of the parent class.

<?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;
?>
Copy after login

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
Copy after login
Copy after login

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">&lt;?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(); ?&gt;</pre><div class="contentsignin">Copy after login</div></div>Result:

Mystatic1
Mystatic1
Copy after login

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();
?>
Copy after login

结果:

Mystatic1
Mystatic2
Copy after login
Copy after login

表明后期静态绑定生效,即 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();
?>
Copy after login

结果:

Mystatic1
Mystatic2
Mystatic1
Mystatic2
Copy after login

同理,self::class 则始终指向的是定义它的类。

静态与非静态的区别

  • 静态属性和方法可以直接通过类引用,所以又被称作类属性和类方法。非静态属性和非静态方法需要实例化后通过对象引用,因此被称作对象属性和对象方法。

  • 静态属性保存在类空间,非静态属性保存在对象空间。非静态方法可以访问类中的任何成员(包括静态),静态方法只能访问类中的静态成员。

  • 静态方法可以直接调用,类名调用和对象调用(类名或self::调用),但是非静态方法只能通过对象调用(对象名或$this->调用)。

  • 一个类的所有实例对象,共用类中的静态属性。如果修改了这个类静态属性,那么这个类的所有对象都能访问到这个新值。

  • 静态方法和属性的生命周期跟相应的类一样长,静态方法和静态属性会随着类的定义而被分配和装载入内存中。一直到线程结束,静态属性和方法才会被销毁。 非静态方法和属性的生命周期和类的实例化对象一样长,只有当类实例化了一个对象,非静态方法和属性才会被创建,而当这个对象被销毁时,非静态方法也马上被销毁。静态方法和静态变量创建后始终使用同一块内存,而使用实例的方式会创建多个内存。但静态方法效率上要比实例化高,静态方法的缺点是不自动进行销毁,而实例化的则可以做销毁。

应用场景:

  1. 静态方法最适合工具类中方法的定义;比如文件操作,日期处理方法等.

  2. 静态变量适合全局变量的定义.

推荐学习:《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!

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