Introduction to static methods and static attributes in php

不言
Release: 2023-04-03 21:26:01
Original
1694 people have browsed it

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!

Related labels:
php
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