Home > Backend Development > PHP Problem > How to call static method in php

How to call static method in php

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

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.

How to call static method in php

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(&#39;a&#39;);
Char::Join();
#注意, 在静态方法中不能访问非静态变量

#Char::Join();
#$test->Join();
#$test::Join();
可以这样调用
Copy after login

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!

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
Latest Issues
Static resource path usage
From 1970-01-01 08:00:00
0
0
0
Why do we need static methods?
From 1970-01-01 08:00:00
0
0
0
Course static resources
From 1970-01-01 08:00:00
0
0
0
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template