Detailed introduction to static methods in PHP

PHPz
Release: 2023-04-19 09:48:45
Original
1049 people have browsed it

In PHP, static method is a special type of method, which is very different from ordinary methods. This article will introduce in detail the concept, usage and precautions of static methods in PHP.

Concept

In PHP, a static method refers to a method that can be called without instantiating an object. In layman's terms, it is an overall operation on the class, rather than an operation on the instantiated object. Use the static keyword flag in front of the method to tell the PHP interpreter that this is a static method.

Usage method

There are several things to note when using static methods:

  1. Call the static method directly through the class name

There is no need to instantiate the object and can be called directly through the class name:

class Example {
    public static function Func(){
        echo "This is a static function!\n"
    }
}

Example::Func(); //Output: This is a static function!
Copy after login
  1. The $this keyword cannot be used in methods

Usually in ordinary methods, we use $ The this keyword is used to reference properties and methods in the class, but in static methods, since there is no instantiated object, the $this keyword cannot be used:

class Example {
    public $name = "example";

    public static function Func(){
        echo "This is a static function! \n";
        //不能使用$this
        //echo $this->name; //错误!
    }
}
Copy after login
  1. You can use the self and static keywords

You can use the self and static keywords in static methods to reference the class itself and the parent class:

class Example {
    public static function Func(){
        echo "This is a static function! \n";
        //使用self引用类本身
        echo "The class name is: ".self::class."\n";
    }
}

class ChildExample extends Example {
    public static function Func() {
        //使用parent关键字引用父类
        parent::Func();
        //使用static关键字引用当前类
        echo "The class name is: ".static::class."\n";
    }
}

ChildExample::Func(); 
//Output: 
//This is a static function! 
//The class name is: Example
//The class name is: ChildExample
Copy after login
  1. can be inherited and overridden

Static methods can be inherited and overridden like ordinary methods:

class Example {
    public static function Func(){
        echo "This is a static function in Example! \n";
    }
}

class ChildExample extends Example {
    public static function Func() {
        echo "This is a static function in ChildExample! \n";
        parent::Func();
    }
}

ChildExample::Func(); 
//Output: 
//This is a static function in ChildExample! 
//This is a static function in Example!
Copy after login

Notes

  1. Static methods can only access static properties

Since static methods do not Instantiated object, so non-static properties cannot be accessed:

class Example {
    public $name = "example";
    public static function Func() {
        echo "The class name is: ".self::class."\n";
        //不能访问非静态属性
        //echo "The name is: ".$this->name."\n";  
        //错误!
    }
}

Example::Func(); //The class name is: Example
Copy after login
  1. Static methods cannot be overridden by non-static methods

In PHP, static methods cannot be overridden by non-static methods Override, and non-static methods cannot be overridden. This is because static methods belong to the entire class, not an object, and cannot be polymorphic.

  1. Static methods should be used as little as possible

Although static methods can provide a lot of convenience, excessive use of static methods is not conducive to the maintainability and scalability of the program. Therefore, we should try to avoid excessive use of static methods in large projects.

Summary

Static method is a special type of method in PHP, which can be called directly without instantiating the object. Static methods can use the self and static keywords to reference the class itself and the parent class, but cannot use the $this keyword. Since static methods belong to the entire class rather than an object, they should be used as little as possible to ensure program maintainability and scalability.

The above is the detailed content of Detailed introduction to static methods in PHP. For more information, please follow other related articles on the PHP Chinese website!

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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!