PHP object-oriented static methods, properties and constants
This article mainly introduces the static methods, properties and constants about PHP object-oriented, which has a certain reference value. Now I share it with you. Friends in need can refer to it
Static methods , attribute
Definition
Use the static
keyword definition;
Declare the class attribute or method as static, that is, you cannot Instantiate, access directly.
Note:
1) Static properties cannot be accessed through instantiated objects;
2) Static methods can be accessed;
3) Static methods , cannot use $this
Usage method
:: 或 self::
The details are as follows:
访问位置 调用属性 调用方法 类的内部/外部 类名::属性名 类名::方法名 内部 self::属性名 self/类名::方法名
Comprehensive example
<?php class MyClass { // 静态属性 public static $a = 'static'; // 静态方法 public static function func1() { echo '静态方法'; // 类的内部调用静态属性 echo MyClass::$a; echo self::$a; // 类的内部调用静态方法 MyClass::func2(); self::func2(); } // 试验静态方法调用另一个静态方法 public static function func2() { echo 'This is static function 2.'; } } // 类的外部调用静态属性、方法 echo MyClass::$a; MyClass::func1(); // 实例化后再调用 $me = new MyClass(); echo $me::$a; // 调用成功 // echo $me ->a; 调用失败 $me -> func1(); // 调用成功
Constant
const
You can define values that remain unchanged in a class as constants.
The value of a constant must be a fixed value.
Call method, same as static.
Example
class MyClass { public static $a = 'abc'; const NUM = 123; } echo MyClass::$a; echo '<br/>'; echo MyClass::NUM; echo '<br/>'; // 修改静态属性 MyClass::$a = 'def'; echo MyClass::$a; echo '<br/>'; // 修改常量 //MyClass::NUM = 234; 赋值失败
Related recommendations:
php object-oriented constructor and destructor
php object-oriented encapsulation
php object-oriented classes and instantiated objects
The above is the detailed content of PHP object-oriented static methods, properties and constants. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

In-depth interpretation of PHP object-oriented encapsulation Encapsulation is one of the three major characteristics of object-oriented programming. It refers to encapsulating data and operations on data in a class, hiding specific implementation details from external programs, and providing external interfaces. In PHP, the concept of encapsulation is implemented by using access modifiers (public, protected, private) to control the accessibility of properties and methods. First, let’s take a look at the role of access modifiers: public (public): Public properties and methods can

How to implement object version control and management through the PHP object-oriented simple factory model. When developing large and complex PHP projects, version control and management are very important. Through appropriate design patterns, we can better manage and control the creation and use of objects, thereby improving the maintainability and scalability of the code. This article will introduce how to use the PHP object-oriented simple factory pattern to implement object version control and management. The simple factory pattern is a design pattern for creating classes that instantiates specified objects through a factory class

How to achieve seamless switching and replacement of objects through PHP's object-oriented simple factory pattern Introduction: In PHP development, object-oriented programming (Object-oriented Programming, referred to as OOP) is a very common programming paradigm. The object-oriented design pattern can further improve the maintainability and scalability of the code. This article will focus on the simple factory pattern in PHP to achieve seamless switching and replacement of objects. What is the simple factory pattern? Simple factory pattern (Simple

How to create flexible object instances using the PHP object-oriented simple factory pattern. The simple factory pattern is a common design pattern that creates object instances without exposing object creation logic. This mode can improve the flexibility and maintainability of the code, and is especially suitable for scenarios where different objects need to be dynamically created based on input conditions. In PHP, we can use the characteristics of object-oriented programming to implement the simple factory pattern. Let's look at an example below. Suppose we need to create a graphing calculator that can

Understanding the Object-Oriented Inheritance Mechanism of PHP Inheritance is an important concept in object-oriented programming, which allows the creation of new classes that include the features and functionality of the old classes. In PHP, inheritance can be achieved through the keyword extends. Through inheritance, subclasses can inherit the properties and methods of the parent class, and can add new properties and methods, or override inherited methods. Let us understand the object-oriented inheritance mechanism of PHP through an example. classAnimal{public$name

With the development of the Internet, PHP has gradually become one of the most popular programming languages in web development. However, with the rapid development of PHP, object-oriented programming has become one of the necessary skills in PHP development. In this article, we will discuss how to master object-oriented programming skills in PHP development. Understanding the Concepts of Object-Oriented Programming Object-oriented programming is a programming paradigm that organizes code and data through the use of objects (classes, properties, and methods). In object-oriented programming, code is organized into reusable modules, thereby improving the program's

With the continuous development of Internet technology, PHP has become one of our common website development languages, and PHP object-oriented programming has also become a knowledge point that must be learned. Object-oriented programming (OOP) is a programming paradigm whose core concept is to combine data and behavior into an object to improve code reusability, readability, and maintainability. This article will explore how to use PHP to implement object-oriented programming and improve the readability and maintainability of your code. Basic Concepts of Object-Oriented Programming In object-oriented programming, each object has a set of properties.

PHP language has become a very popular web development language because it is easy to learn and use. Object-oriented programming is one of the most important programming paradigms in the PHP language. However, object-oriented programming is not something that is easy to master, so some common problems often arise. In this article, we will provide a detailed analysis of common problems with object-oriented programming in PHP. Question 1: How to create an object? In PHP, you can use the new keyword to create an object. For example: classMyClass{/
