search
Homephp教程php手册PHP 中 new static 和 new self 的区别,staticself

PHP 中 new static 和 new self 的区别,staticself

        今天老大在公司 问了一下  new static  和 new self 的区别 公司十个程序 竟然没有一个回答上来 后面画面自补 。。。

 

本屌丝回家后 就百度了解了下 这二者区别 :

使用 self:: 或者 __CLASS__ 对当前类的静态引用,取决于定义当前方法所在的类:

使用 static:: 不再被解析为定义当前方法所在的类,而是在实际运行时计算的。也可以称之为“静态绑定”,因为它可以用于(但不限于)静态方法的调用。

 

简单通俗的来说, self就是写在哪个类里面, 实际调用的就是这个类.所谓的后期静态绑定, static代表使用的这个类, 就是你在父类里写的static,

然后通过子类直接/间接用到了这个static, 这个static指的就是这个子类, 所以说static和$this很像, 但是static可以用于静态方法和属性等.

请看列子

 

 

<?<span>php
</span><span>class</span><span> Person
{
    </span><span>public</span> <span>static</span> <span>function</span><span> name()
    {
        </span><span>echo</span> "xiaosan"<span>;
    }
    </span><span>public</span> <span>static</span> <span>function</span><span> callself()
    {
        self</span>::<span>name();
    }

    </span><span>public</span> <span>static</span> <span>function</span><span> callstatic()
    {
        </span><span>static</span>::<span>name();
    }
}

</span><span>class</span> Man <span>extends</span><span> Person
{
    </span><span>public</span> <span>static</span> <span>function</span><span> name()
    {
        </span><span>echo</span> "gaojin"<span>;
    }
}
Man</span>::name();  <span>//</span><span> output: gaojin</span>
Person::callself();  <span>//</span><span> output: xiaosan</span>
Person::callstatic();  <span>//</span><span> output:gaojin</span>
?>

 

  小编继续学习中 

 

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

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

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!