PHP 中 new static 跟 new self 的区别
Jun 13, 2016 pm 12:23 PM
PHP 中 new static 和 new self 的区别
今天老大在公司 问了一下 new static 和 new self 的区别 公司十个程序 竟然没有一个回答上来 后面画面自补 。。。
本屌丝回家后 就百度了解了下 这二者区别 :
使用 self:: 或者 __CLASS__ 对当前类的静态引用,取决于定义当前方法所在的类:
使用 static:: 不再被解析为定义当前方法所在的类,而是在实际运行时计算的。也可以称之为“静态绑定”,因为它可以用于(但不限于)静态方法的调用。
简单通俗的来说, self就是写在哪个类里面, 实际调用的就是这个类.所谓的后期静态绑定, static代表使用的这个类, 就是你在父类里写的static,
然后通过子类直接/间接用到了这个static, 这个static指的就是这个子类, 所以说static和$this很像, 但是static可以用于静态方法和属性等.
请看列子
<span style="color: #000000;">php</span><span style="color: #0000ff;">class</span><span style="color: #000000;"> Person{ </span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">static</span> <span style="color: #0000ff;">function</span><span style="color: #000000;"> name() { </span><span style="color: #0000ff;">echo</span> "xiaosan"<span style="color: #000000;">; } </span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">static</span> <span style="color: #0000ff;">function</span><span style="color: #000000;"> callself() { self</span>::<span style="color: #000000;">name(); } </span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">static</span> <span style="color: #0000ff;">function</span><span style="color: #000000;"> callstatic() { </span><span style="color: #0000ff;">static</span>::<span style="color: #000000;">name(); }}</span><span style="color: #0000ff;">class</span> Man <span style="color: #0000ff;">extends</span><span style="color: #000000;"> Person{ </span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">static</span> <span style="color: #0000ff;">function</span><span style="color: #000000;"> name() { </span><span style="color: #0000ff;">echo</span> "gaojin"<span style="color: #000000;">; }}Man</span>::name(); <span style="color: #008000;">//</span><span style="color: #008000;"> output: gaojin</span>Person::callself(); <span style="color: #008000;">//</span><span style="color: #008000;"> output: xiaosan</span>Person::callstatic(); <span style="color: #008000;">//</span><span style="color: #008000;"> output:gaojin</span>?>
小编继续学习中

Article chaud

Outils chauds Tags

Article chaud

Tags d'article chaud

Bloc-notes++7.3.1
Éditeur de code facile à utiliser et gratuit

SublimeText3 version chinoise
Version chinoise, très simple à utiliser

Envoyer Studio 13.0.1
Puissant environnement de développement intégré PHP

Dreamweaver CS6
Outils de développement Web visuel

SublimeText3 version Mac
Logiciel d'édition de code au niveau de Dieu (SublimeText3)

Sujets chauds

Solution : Votre organisation vous demande de modifier votre code PIN

Comment ajuster les paramètres de bordure de fenêtre sous Windows 11 : modifier la couleur et la taille

Comment changer la couleur de la barre de titre sous Windows 11 ?

Comment activer ou désactiver les aperçus miniatures de la barre des tâches sur Windows 11

Problèmes d'erreur OOBELANGUAGE dans la réparation de Windows 11/10

Afficher le guide de mise à l'échelle sur Windows 11

10 façons de régler la luminosité sous Windows 11

Comment désactiver l'authentification de navigation privée pour iPhone dans Safari ?
