Home > Article > Backend Development > Introduction to static methods and static attributes in php
This article brings you an introduction to static methods and static attributes in PHP. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.
Static is divided into two parts: static properties and static methods
Static things are for classes (including class constants), and non-static things are for objects. The
When defining an attribute, the attribute modified with the keyword static is called static Attributes.
The method modified with the static keyword is called a static method.
Problems that can be solved statically do not need to be solved by ordinary methods (static things can be accessed directly using classes and do not need to be instantiated through objects, thus saving memory space and improving efficiency).
Special Note:
In the process of writing code, strictly follow the capitalization specifications
Strictly follow: Static content is accessed by classes, non-static content is accessed by objects, no cross-border operations are allowed
1. self represents the class and $this represents the object
2. self accesses the constants, static properties and static methods inside the class, $this accesses the properties and common methods of the object
3. self must cooperate with the range resolution operator (::) To take effect, $this represents an object. The object is inherently a data type, so $this can be printed alone.
4. Where you can use $this, you can definitely use self, but where you can use self, you may not be able to use $this
5. Non-static attributes and methods are accessed using objects ($this), and static methods, properties or constants are accessed using classes (self)
Related recommendations:
static in php object-oriented Calling attributes and static methods_PHP tutorial
PHP method to achieve static HTML page, phphtml page static
The above is the detailed content of Introduction to static methods and static attributes in php. For more information, please follow other related articles on the PHP Chinese website!