php中的static
静态成员是一种类变量,可以把它看成时属于整个类而不是属于类的某个实例。与一般的实例变量不同的是,静态成员只保留一个变量值,而这个变量值对所有的实例都是有效的,也就是说,所有的实例共享这个成员。
$this只表示类的当前实例,而 self:: 表示的是类本身,在类之外的代码中不能使用这个操作符,而且它不能识别自己在继承树层次结构中的位置。也就是说,在扩展类中使用self 作用域时,self 可以调用基类中声明的方法,但它调用的总是已经在扩展类中重写的方法。与$this不同的是,在使用静态变量时,必须在作用域限定符后面加上$符号。
在扩展类中,在基类的方法被重写的情况下,使用 parent 作用域调用定义在基类中的方法。静态成员也可以只属于父类。如果在子类和父类中同时声明了某个成员,也可以使用parant::在子类中访问父类中的变量。在这种情况下,父类的静态成员和子类的静态成员保存的是不同的值。
可以在 :: 操作符的左边写上类的名称来静态地访问某个成员,这样避免创建类的实例。不仅省略掉实例化类的代码,而且还会更高效,因为类的每个实例都会占用一小部分的系统资源。
在使用 :: 操作符访问成员变量时,需要再次注意对$符号的使用。因为PHP当前不支持动态的静态变量的使用,也就是说不支持可变的静态变量。在使用$this->$var时,被访问的成员是包含在$var中的变量的值。而不用$符号访问某个变量实际上查找的是类的某个常量,而常量是不能通过$this来访问的。
PHP6中提出的static::作用域使我们不再需要使用self::和parent::。当希望指向最终的实现功能的类时,就可以使用static::,这个限定符会在代码执行之前立即计算出继承层次机构上最后那个类的成员。之一过程被称为延迟绑定,它使我们可以在子类中重写某个静态变量,并且也可以从某个在父类中声明的函数中反问这个最终成员。
有时,可能有必要创建供所有类实例共享的字段和方法,这些字段和方法与所有的类实例有关,但不能由任何特定对象调用。例如,假设要编写一个类,跟踪网页访问者的数量。你一定不希望每次实例化该类时都把访问者数量重置为0 ,此时就可以将该字段设置为static作用域:
<?php class visitors { private static $visitors = 0; function __construct() { self::$visitors++; } static function getVisitors() { return self::$visitors; } } /* Instantiate the visitors class. */ $visits = new visitors(); echo visitors::getVisitors()."<br/>"; /* Instantiate another visitors class. */ $visits2 = new visitors(); echo visitors::getVisitors()."<br/>"; ?>
程序运行结果:
1
2
因为$visitors字段声明为static,所以对其值的任何改变都会反映到所有实例化对象中。还要注意,静态字段和方法应使用self关键字和类名来引用,而不是通过this和箭头操作符。这是因为使用“正常”方法引用静态字段是不可能的,会导致语法错误。
不能在类中使用$this来引用为static字段。
静态变量
静态变量是只存在于函数作用域的变量,不过,在函数执行完成后,这种变量的值不会丢失,也就是说,在下一次调用这个函数时,变量仍然会记得原来的值。要将某个变量定义为静态的,只需要在变量前加上 static 关键字即可。
在类中,static关键字有两种主要用法,一是用来定义静态成员,一是用来定义静态方法。在类的内部,可以使用作用域限定符 (::) 来访问不同层次作用域的变量。
静态方法
静态方法和非静态方法之间有一个重要的区别:在调用静态方法时,不再需要拥有类的实例。
静态方法和非静态方法使用原则:一是如果某个方法中不包含$this变量,就应该时静态方法;如果不需要类的实例,可能还应该使用静态类,这样可以免去实例化类的工作。另,在静态方法中时不能使用$this变量的,因为静态方法不属于某个特定的实例。

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











The reason for the error is NameResolutionError(self.host,self,e)frome, which is an exception type in the urllib3 library. The reason for this error is that DNS resolution failed, that is, the host name or IP address attempted to be resolved cannot be found. This may be caused by the entered URL address being incorrect or the DNS server being temporarily unavailable. How to solve this error There may be several ways to solve this error: Check whether the entered URL address is correct and make sure it is accessible Make sure the DNS server is available, you can try using the "ping" command on the command line to test whether the DNS server is available Try accessing the website using the IP address instead of the hostname if behind a proxy

Before introducing the usage of self in Python, let’s first introduce the classes and instances in Python. We know that the most important concepts of object-oriented are classes and instances. Classes are abstract templates, such as abstract things like students. , can be represented by a Student class. Instances are specific "objects" created based on classes. Each object inherits the same methods from the class, but its data may be different. 1. Take the Student class as an example. In Python, the class is defined as follows: classStudent(object):pass(Object) indicates which class the class inherits from. The Object class is all

This article will help you interpret the vue source code and introduce why you can use this to access properties in various options in Vue2. I hope it will be helpful to everyone!

Inobject-orientedprogramming,inheritanceallowsustocreatenewclassesthatinheritthepropertiesandmethodsofanexistingclass.Thispowerfulconceptenablescodereuse,modularity,andextensibilityinourprograms.Beforedivingintoaccessingparentclassattributes,let'shav

A colleague got stuck due to a bug pointed by this. Vue2’s this pointing problem caused an arrow function to be used, resulting in the inability to get the corresponding props. He didn't know it when I introduced it to him, and then I deliberately looked at the front-end communication group. So far, at least 70% of front-end programmers still don't understand it. Today I will share with you this link. If everything is wrong If you haven’t learned it yet, please give me a big mouth.

The role and usage of static in C language: 1. Variable scope; 2. Life cycle; 3. Internal function; 4. Modify global variables; 5. Modify function; 6. Other uses; Detailed introduction: 1. Variable scope, when If there is the static keyword before a variable, then the scope of the variable is limited to the file in which it is declared. In other words, the variable is a "file-level scope", which is very useful for preventing the "duplicate definition" problem of variables; 2. Life cycle, static variables are initialized once when the program starts executing, and destroyed when the program ends, etc.

Practical application scenarios and usage skills of the static keyword in C language 1. Overview static is a keyword in C language, used to modify variables and functions. Its function is to change its life cycle and visibility during program running, making variables and functions static. This article will introduce the actual application scenarios and usage techniques of the static keyword, and illustrate it through specific code examples. 2. Static variables extend the life cycle of variables. Using the static keyword to modify local variables can extend their life cycle.

1. static Please look at the following program first: publicclassHello{publicstaticvoidmain(String[]args){//(1)System.out.println("Hello, world!");//(2)}} Have seen this Segment programs are familiar to most people who have studied Java. Even if you have not learned Java but have learned other high-level languages, such as C, you should be able to understand the meaning of this code. It simply outputs "Hello, world" and has no other use. However, it shows the main purpose of the static keyword.
