Home  >  Article  >  Backend Development  >  Introduction to static methods and static attributes in php

Introduction to static methods and static attributes in php

不言
不言Original
2018-08-22 15:47:211702browse

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

static attribute

When defining an attribute, the attribute modified with the keyword static is called static Attributes.

Static method

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:

  1. In the process of writing code, strictly follow the capitalization specifications

  2. Strictly follow: Static content is accessed by classes, non-static content is accessed by objects, no cross-border operations are allowed

3. The difference between self and $this

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!

Statement:
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