第七节--类的静态成员
/*
+-------------------------------------------------------------------------------+
| = 本文为Haohappy读>
| = 中Classes and Objects一章的笔记
| = 翻译为主+个人心得
| = 为避免可能发生的不必要的麻烦请勿转载,谢谢
| = 欢迎批评指正,希望和所有PHP爱好者共同进步!
| = PHP5研究中心: http://blog.csdn.net/haohappy2004
+-------------------------------------------------------------------------------+
*/
第七节--类的静态成员
类的静态成员与一般的类成员不同: 静态成员与对象的实例无关,只与类本身有关. 他们用来实现类要封装的功能和数据,但不包括特定对象的功能和数据. 静态成员包括静态方法和静态属性.
静态属性包含在类中要封装的数据,可以由所有类的实例共享. 实际上,除了属于一个固定的类并限制访问方式外,类的静态属性非常类似于函数的全局变量
我们在下例中使用了一个静态属性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 set up keyboard startup on Gigabyte's motherboard. First, if it needs to support keyboard startup, it must be a PS2 keyboard! ! The setting steps are as follows: Step 1: Press Del or F2 to enter the BIOS after booting, and go to the Advanced (Advanced) mode of the BIOS. Ordinary motherboards enter the EZ (Easy) mode of the motherboard by default. You need to press F7 to switch to the Advanced mode. ROG series motherboards enter the BIOS by default. Advanced mode (we use Simplified Chinese to demonstrate) Step 2: Select to - [Advanced] - [Advanced Power Management (APM)] Step 3: Find the option [Wake up by PS2 keyboard] Step 4: This option The default is Disabled. After pulling down, you can see three different setting options, namely press [space bar] to turn on the computer, press group

1. Processor When choosing a computer configuration, the processor is one of the most important components. For playing games like CS, the performance of the processor directly affects the smoothness and response speed of the game. It is recommended to choose Intel Core i5 or i7 series processors because they have powerful multi-core processing capabilities and high frequencies, and can easily cope with the high requirements of CS. 2. Graphics card Graphics card is one of the important factors in game performance. For shooting games such as CS, the performance of the graphics card directly affects the clarity and smoothness of the game screen. It is recommended to choose NVIDIA GeForce GTX series or AMD Radeon RX series graphics cards. They have excellent graphics processing capabilities and high frame rate output, and can provide a better gaming experience. 3. Memory power

SPDIFOUT connection line sequence on the motherboard. Recently, I encountered a problem regarding the wiring sequence of the wires. I checked online. Some information says that 1, 2, and 4 correspond to out, +5V, and ground; while other information says that 1, 2, and 4 correspond to out, ground, and +5V. The best way is to check your motherboard manual. If you can't find the manual, you can use a multimeter to measure it. Find the ground first, then you can determine the order of the rest of the wiring. How to connect motherboard VDG wiring When connecting the VDG wiring of the motherboard, you need to plug one end of the VGA cable into the VGA interface of the monitor and the other end into the VGA interface of the computer's graphics card. Please be careful not to plug it into the motherboard's VGA port. Once connected, you can

Glodon Software is a software company focusing on the field of building informatization. Its products are widely used in all aspects of architectural design, construction, and operation. Due to the complex functions and large data volume of Glodon software, it requires high computer configuration. This article will elaborate on the computer configuration recommendations of Glodon Software from many aspects to help readers choose a suitable computer configuration processor. Glodon Software requires a large amount of data calculation and processing when performing architectural design, simulation and other operations. Therefore, the requirements for the processor are higher. It is recommended to choose a multi-core, high-frequency processor, such as Intel i7 series or AMD Ryzen series. These processors have strong computing power and multi-thread processing capabilities, and can better meet the needs of Glodon software. Memory Memory is affecting computing

In-depth analysis of the role and usage of the static keyword in C language. In C language, static is a very important keyword, which can be used in the definition of functions, variables and data types. Using the static keyword can change the link attributes, scope and life cycle of the object. Let’s analyze the role and usage of the static keyword in C language in detail. Static variables and functions: Variables defined using the static keyword inside a function are called static variables, which have a global life cycle

Which version of the graphics card driver is best to use? 1. There is no absolute best version. It is most important to choose the version that suits your computer; 2. Because the applicability and stability of the graphics card driver version are related to the computer hardware environment and system configuration; 3. You can check the detailed information of the computer and graphics card on the official website, select the appropriate driver version based on the information, or refer to the reviews of other users. It is recommended to back up the system before installing the driver to avoid unexpected situations. Graphics card driver version 472.19 series is an excellent choice. Currently, the driver compatibility of version 472 is the best. Installing version 472 of the driver can also maximize the performance of the graphics card. The NVIDIA graphics card driver Win7 installation version, numbered 2, 472.19, is a product with remarkable quality.

The role and application scenarios of private static methods in PHP In PHP programming, a private static method is a special method type. It can only be accessed within the class in which it is defined and cannot be directly called from the outside. Private static methods are usually used for the internal logic implementation of a class, providing a way to encapsulate and hide details. At the same time, they have the characteristics of static methods and can be called without instantiating the class object. The following will discuss the role and application scenarios of private static methods, and provide specific code examples. Function: encapsulate and hide implementation details: private static

I am planning to go backpacking in Tibet. ① How many liters of bag should I carry? Please tell me what you think is the best configuration. I am 170 and have good physical strength. The first time I went hiking, the amount was 60 liters or more. The amount of hiking was less than 60 liters. The entire journey was by car. You don’t need a backpack, a suitcase is more convenient. If you really need to carry something with you, a 25-40 liter bag is more than enough. Necessary supplies for Tibet travel: sunglasses, sun hat, sunscreen, skin cream, lip balm, long-sleeved top, Sweater; for special travel or travel to Ali, northern Tibet, and Sichuan-Tibet line, it is recommended to bring: sleeping bag (cold protection), sheets (dirty protection), down jacket, travel shoes or hiking shoes, slippers, toothbrush, toothpaste, towel, rolling paper , paper underwear, disinfectant wipes, flashlight, waterproof matches, knives, rope. Can a computer be carried in the front bag? Can a computer be carried in the front bag? Some backpacks have it.
