Join();$test::Join();" can be called."/> Join();$test::Join();" can be called.">
Home > Article > Backend Development > How to call static method in php
php calls the static method: 1. Create a PHP sample file; 2. Define "class Char{...}"; 3. Create "public static function Join(){...}"; 4. , use "Char::Join();$test->Join();$test::Join();" to call.
The operating environment of this tutorial: Windows 7 system, PHP version 8.1, Dell G3 computer.
How to call static method in php?
Using the static method in php
The code is as follows:
<?php class Char{ public static $number = 0; public static $name; function __construct($what){ self::$name = $what; } public static function Join(){ self::$number++; echo self::$number," Is :",self::$name,"<br />"; } } $test = new Char('a'); Char::Join(); #注意, 在静态方法中不能访问非静态变量 #Char::Join(); #$test->Join(); #$test::Join(); 可以这样调用
Related introduction:
Static attributes
1: static static attributes exist alone in the class (belong to the class) and do not belong to the object. Therefore, this attribute exists as long as the class is declared. You can access this static property without relying on an object.
2: There is always a static property in the class, so it is shared by all objects. One person affects it and the others share it.
Static method
Ordinary methods are stored in categories and have only one copy in memory. The same goes for static methods. Difference: Ordinary methods require objects to call and this needs to be bound to this. Static methods do not need to be bound to this. Static methods do not need to bind this. Static methods do not need to be bound to this, so they can be called through the class name
Recommended learning: "PHP Video Tutorial"
The above is the detailed content of How to call static method in php. For more information, please follow other related articles on the PHP Chinese website!