search
HomeBackend DevelopmentPHP TutorialSection 7 - Static members of classes - Classes and Objects in PHP5 [7]_PHP Tutorial

Section 7 - Static members of a class
Static members of a class are different from ordinary class members: static members have nothing to do with the instance of the object, only the class itself. They are used to implement the functions and data that the class wants to encapsulate, but Excludes the functionality and data of a specific object. Static members include static methods and static properties.
Static properties contain data to be encapsulated in a class and can be shared by all instances of the class. In fact, in addition to belonging to a fixed class and In addition to restricting access, the static properties of a class are very similar to the global variables of a function
We use a static property Counter::$count in the following example. It belongs to the Counter class and does not belong to any Counter instance. You cannot use this to refer to it, but you can use self or other valid naming expressions. In the example, the getCount method returns self::$count, not Counter::$count.
The static method implements the functions that the class needs to encapsulate, It has nothing to do with a specific object. Static methods are very similar to global functions. Static methods can fully access the properties of the class, or they can be accessed by instances of the object, regardless of the access qualifier.
In example 6.3, getCount is an ordinary method, called with ->. PHP creates a this variable, although the method is not used. However, getCount does not belong to any object. In some cases, we even want to call it when there is no valid object , then static methods should be used. PHP will not create this variable inside static methods, even if you call them from an object.
Example 6.7 is derived from 6.3 changing getCount to a static method. The Static keyword cannot prevent an instance from using -> operator to call getCount, but PHP will not create this variable inside the method. If you use this-> to call, an error will occur.
//6.3 Example refers to Section 4 - Constructor and Analysis The example in the constructor (see the previous article), by comparing the two examples, you can easily grasp the difference between
//static method and ordinary method.
You can write a method to determine whether this is established to show whether it is called statically or non-statically. Of course, if you use the static keyword, this method will always be static no matter how it is called.
Your class can also define constant attributes, no need To use public static, just use the const keyword. Constant properties are always static. They are properties of the class, not the properties of the object that instantiates the class.
Listing 6.7 Static members
php
class Counter
{
private static $count = 0;
const VERSION = 2.0;
function __construct()
{
self: :$count++;

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/445275.htmlTechArticleSection 7 - Static members of classes Static members of classes are different from ordinary class members: Static members and objects The instances have nothing to do with, only the class itself. They are used to implement the functions and data to be encapsulated by the class...
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
php5和php8有什么区别php5和php8有什么区别Sep 25, 2023 pm 01:34 PM

php5和php8的区别在性能、语言结构、类型系统、错误处理、异步编程、标准库函数和安全性等方面。详细介绍:1、性能提升,PHP8相对于PHP5来说在性能方面有了巨大的提升,PHP8引入了JIT编译器,可以对一些高频执行的代码进行编译和优化,从而提高运行速度;2、语言结构改进,PHP8引入了一些新的语言结构和功能,PHP8支持命名参数,允许开发者通过参数名而不是参数顺序等等。

如何在技嘉主板上设置键盘启动功能 (技嘉主板启用键盘开机方式)如何在技嘉主板上设置键盘启动功能 (技嘉主板启用键盘开机方式)Dec 31, 2023 pm 05:15 PM

技嘉的主板怎么设置键盘开机首先,要支持键盘开机,一定是PS2键盘!!设置步骤如下:第一步:开机按Del或者F2进入bios,到bios的Advanced(高级)模式普通主板默认进入主板的EZ(简易)模式,需要按F7切换到高级模式,ROG系列主板默认进入bios的高级模式(我们用简体中文来示范)第二步:选择到——【高级】——【高级电源管理(APM)】第三步:找到选项【由PS2键盘唤醒】第四步:这个选项默认是Disabled(关闭)的,下拉之后可以看到三种不同的设置选择,分别是按【空格键】开机、按组

CS玩家的首选:推荐的电脑配置CS玩家的首选:推荐的电脑配置Jan 02, 2024 pm 04:26 PM

1.处理器在选择电脑配置时,处理器是至关重要的组件之一。对于玩CS这样的游戏来说,处理器的性能直接影响游戏的流畅度和反应速度。推荐选择IntelCorei5或i7系列的处理器,因为它们具有强大的多核处理能力和高频率,可以轻松应对CS的高要求。2.显卡显卡是游戏性能的重要因素之一。对于射击游戏如CS而言,显卡的性能直接影响游戏画面的清晰度和流畅度。建议选择NVIDIAGeForceGTX系列或AMDRadeonRX系列的显卡,它们具备出色的图形处理能力和高帧率输出,能够提供更好的游戏体验3.内存电

主板上的数字音频输出接口-SPDIF OUT主板上的数字音频输出接口-SPDIF OUTJan 14, 2024 pm 04:42 PM

主板上SPDIFOUT连接线序最近我遇到了一个问题,就是关于电线的接线顺序。我上网查了一下,有些资料说1、2、4对应的是out、+5V、接地;而另一些资料则说1、2、4对应的是out、接地、+5V。最好的办法是查看你的主板说明书,如果找不到说明书,你可以使用万用表进行测量。首先找到接地,然后就可以确定其他的接线顺序了。主板vdg怎么接线连接主板的VDG接线时,您需要将VGA连接线的一端插入显示器的VGA接口,另一端插入电脑的显卡VGA接口。请注意,不要将其插入主板的VGA接口。完成连接后,您可以

广联达软件电脑配置推荐;广联达软件对电脑的配置要求广联达软件电脑配置推荐;广联达软件对电脑的配置要求Jan 01, 2024 pm 12:52 PM

广联达软件是一家专注于建筑信息化领域的软件公司,其产品被广泛应用于建筑设计、施工、运营等各个环节。由于广联达软件功能复杂、数据量大,对电脑的配置要求较高。本文将从多个方面详细阐述广联达软件的电脑配置推荐,以帮助读者选择适合的电脑配置处理器广联达软件在进行建筑设计、模拟等操作时,需要进行大量的数据计算和处理,因此对处理器的要求较高。推荐选择多核心、高主频的处理器,如英特尔i7系列或AMDRyzen系列。这些处理器具有较强的计算能力和多线程处理能力,能够更好地满足广联达软件的需求。内存内存是影响计算

深入解析C语言中static关键字的作用和用法深入解析C语言中static关键字的作用和用法Feb 20, 2024 pm 04:30 PM

深入解析C语言中static关键字的作用和用法在C语言中,static是一种非常重要的关键字,它可以被用于函数、变量和数据类型的定义上。使用static关键字可以改变对象的链接属性、作用域和生命周期,下面就来详细地解析一下static关键字在C语言中的作用和用法。static变量和函数:在函数内部使用static关键字定义的变量称为静态变量,它具有全局生命周

我准备去西藏旅行背包去①背多少升的包合适把你认为最好的配置说下本人170体力不错第一次我准备去西藏旅行背包去①背多少升的包合适把你认为最好的配置说下本人170体力不错第一次Jan 07, 2024 am 10:06 AM

我准备去西藏旅行背包去①背多少升的包合适把你认为最好的配置说下本人170体力不错第一次去徒步多就60升或以上的徒步少就60升以下的全程都坐车就不用背包,旅行箱更方便,真要随身带东西,弄个25~40升的就绰绰有馀西藏旅游必备用品:太阳镜、太阳帽、防晒霜、护肤霜、润唇膏、长袖上衣、毛衣;对于特殊旅游或去阿里、藏北、川藏线旅游,建议带:睡袋(防寒)、床单(防脏)、羽绒服、旅游鞋或登山鞋、拖鞋、牙刷、牙膏、毛巾、卷筒纸、纸内裤、消毒湿巾、手电筒、防水火柴、刀具、绳子。前运包能装电脑吗能装电脑,有些背包有

php5如何改80端口php5如何改80端口Jul 24, 2023 pm 04:57 PM

php5改80端口的方法:1、编辑Apache服务器的配置文件中的端口号;2、辑PHP的配置文件以确保PHP在新端口上工作;3、重启Apache服务器,PHP应用程序将开始在新的端口上运行。

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version