第七节 类的静态成员 [7]
类的静态成员与一般的类成员不同: 静态成员与对象的实例无关,只与类本身有关. 他们用来实现类要封装的功能和数据,但不包括特定对象的功能和数据. 静态成员包括静态方法和静态属性.
静态属性包含在类中要封装的数据,可以由所有类的实例共享. 实际上,除了属于一个固定的类并限制访问方式外,类的静态属性非常类似于函数的全局变量
我们在下例中使用了一个静态属性Counter::$count. 它属于Counter类,而不属于任何Counter的实例.你不能用this来引用它,但可以用self或其它有效的命名表达. 在例子中,getCount方法返回self::$count,而不是Counter::$count.
静态方法则实现类需要封装的功能,与特定的对象无关. 静态方法非常类似于全局函数. 静态方法可以完全访问类的属性,也可以由对象的实例来访问,不论访问的限定语是否是什么.
在6.3例中,getCount是一个普通的方法,用->来调用. PHP建立一个this变量,尽管方法没有使用到.但是,getCount不属于任何对象.在有些情况下,我们甚至希望在不存在有效的对象时调用它,那么就应该使用静态方法. PHP将不在静态方法内部建立this变量,即使你从一个对象中调用它们.
例子6.7由6.3改变getCount为静态方法而来. Static关键字不能阻止一个实例用->运算符来调用getCount,但PHP将不在方法内部建立this变量.如果你使用this->来调用,将会出错.
//6.3例指第四节--构造函数和析构函数中的例子(参看前文),通过两个例子的比较,你可以很好掌握
//static方法与普通方法之间的区别.
你可以写一个方法通过判断this是否建立来显示是否它被静态地或者非静态地调用. 当然,如果你用了static 关键字,不管它怎样被调用,这个方法总是静态的.
你的类也可以定义常量属性,不需要使用public static,只需要用const关键字即可. 常量属性总是静态的.它们是类的属性,而不是实例化该类的对象的属性.
Listing 6.7 Static members
class Counter
{
private static $count = 0;
const VERSION = 2.0;
function __construct()
{
self::$count++;
}
function __destruct()
{
self::$count--;
}
static function getCount()
{
return self::$count;
}
};
//创建一个实例,则__construct()将执行
$c = new Counter();
//输出 1
print(Counter::getCount() . "
n");
//输出类的版本属性
print("Version used: " . Counter::VERSION . "
n");
?>

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

How to implement dual WeChat login on Huawei mobile phones? With the rise of social media, WeChat has become one of the indispensable communication tools in people's daily lives. However, many people may encounter a problem: logging into multiple WeChat accounts at the same time on the same mobile phone. For Huawei mobile phone users, it is not difficult to achieve dual WeChat login. This article will introduce how to achieve dual WeChat login on Huawei mobile phones. First of all, the EMUI system that comes with Huawei mobile phones provides a very convenient function - dual application opening. Through the application dual opening function, users can simultaneously

The programming language PHP is a powerful tool for web development, capable of supporting a variety of different programming logics and algorithms. Among them, implementing the Fibonacci sequence is a common and classic programming problem. In this article, we will introduce how to use the PHP programming language to implement the Fibonacci sequence, and attach specific code examples. The Fibonacci sequence is a mathematical sequence defined as follows: the first and second elements of the sequence are 1, and starting from the third element, the value of each element is equal to the sum of the previous two elements. The first few elements of the sequence

How to implement the WeChat clone function on Huawei mobile phones With the popularity of social software and people's increasing emphasis on privacy and security, the WeChat clone function has gradually become the focus of people's attention. The WeChat clone function can help users log in to multiple WeChat accounts on the same mobile phone at the same time, making it easier to manage and use. It is not difficult to implement the WeChat clone function on Huawei mobile phones. You only need to follow the following steps. Step 1: Make sure that the mobile phone system version and WeChat version meet the requirements. First, make sure that your Huawei mobile phone system version has been updated to the latest version, as well as the WeChat App.

In today's software development field, Golang (Go language), as an efficient, concise and highly concurrency programming language, is increasingly favored by developers. Its rich standard library and efficient concurrency features make it a high-profile choice in the field of game development. This article will explore how to use Golang for game development and demonstrate its powerful possibilities through specific code examples. 1. Golang’s advantages in game development. As a statically typed language, Golang is used in building large-scale game systems.

PHP Game Requirements Implementation Guide With the popularity and development of the Internet, the web game market is becoming more and more popular. Many developers hope to use the PHP language to develop their own web games, and implementing game requirements is a key step. This article will introduce how to use PHP language to implement common game requirements and provide specific code examples. 1. Create game characters In web games, game characters are a very important element. We need to define the attributes of the game character, such as name, level, experience value, etc., and provide methods to operate these

Here's how to convert a MySQL query result array into an object: Create an empty object array. Loop through the resulting array and create a new object for each row. Use a foreach loop to assign the key-value pairs of each row to the corresponding properties of the new object. Adds a new object to the object array. Close the database connection.

PHP functions can encapsulate data into a custom structure by returning an object using a return statement followed by an object instance. Syntax: functionget_object():object{}. This allows creating objects with custom properties and methods and processing data in the form of objects.

In PHP, an array is an ordered sequence, and elements are accessed by index; an object is an entity with properties and methods, created through the new keyword. Array access is via index, object access is via properties/methods. Array values are passed and object references are passed.
