首页 > 编程语言 >PHP中self与static区别和联系

PHP中self与static区别和联系

时间:2022-11-21 13:03:00浏览次数:41  
标签:function PHP get self static Foo class


 

PHP官方也说过,大概意思是说self调用的就是本身代码片段这个类,static - PHP 5.3加进来的只得是当前这个类,有点像$this的意思,static调用的是从堆内存中提取出来,访问的是当前实例化的那个类,那么 static 代表的就是那个类,例子比较容易明白些。

其实static就是调用的当前调用的类,比较抽象吧。

 

self refers to the same class whose method the new operation takes place in.

static in PHP 5.3's late static bindings refers to whatever class in the hierarchy which you call the method on.

In the following example, B inherits both methods from A. self is bound to A because it's defined in A's implementation of the first method, whereas static is bound to the called class (also see  get_called_class() )

<?php

class Boo {

protected static $str = "This is class Boo";

/*

public static function get_info(){

echo get_called_class()."<br>"; //output Foo

echo self::$str; //output This is class Boo

}

*/

public static function get_info(){

echo get_called_class()."<br>"; //output Foo

echo static::$str; //output This is class Foo

}

}

class Foo extends Boo{

protected static $str = "This is class Foo";

}

Foo::get_info();

?>

 

再举个例子:

class A {

public static function get_self() {

return new self();

}



public static function get_static() {

return new static();

}

}



class B extends A {}



echo get_class(B::get_self()); // A

echo get_class(B::get_static()); // B

echo get_class(A::get_static()); // A

 

 

标签:function,PHP,get,self,static,Foo,class
From: https://blog.51cto.com/u_6353447/5873550

相关文章

  • PHP中如何实现进程间通讯
    ​ 进程间通讯机制——IPC(Inter-Process-Communication)。为了使得php5可以使用共享内存和信号量,必须在编译php5程序时激活shmop和sysvsem这两个扩展模块。实现方法:在......
  • PHP一些基础语法,转菜鸟
    数组使用①:遍历关联数组for($arrayas$key=>$value){echo$key.'=>'.$value.PHP_EOL;}②:判断key是否存在boolarray_key_exists(key,array)③:去除(shift)......
  • php – Laravel – 从附带where子句的另一个表中不存在的记录中获取记录
    1.存在一个商户表id  name 2.存在一个会员表idadmin_idname3.查询还没有添加会员的商户信息DB::table('admin_users')->leftJoin('admin_ext','admin_ext.adm......
  • php开发中实现客户端扫描文档并保存到服务器端
    项目中需要控制扫描仪器扫描文稿并作为OA系统的附件保存到服务器端,在网页中控制扫描仪使用的是ScanOnWeb控件,实现了客户端通过javascript控制扫描仪扫描文稿,多页文稿全部扫......
  • php使用form-create、FormBuilder快捷创建表单
    form-create、FormBuilder不用不知道,一用吓一跳!好用还漂亮说明一下,form-create主要是用于前段的,FormBuilder是大神基于form-create写的能够用php写前端表单的,我在B站也简单......
  • 为什么你的static_assert不能按预期的工作?
    static_assert是c++11添加的新语法,它可以使我们在编译期间检测一些断言条件是否为真,如果不满足条件将会产生一条编译错误信息。使用静态断言可以提前暴露许多问题到编译阶......
  • static-静态方法和工具类
    什么叫静态方法特点和调用方式工具类定义:帮助我们做一些事情的,但是不描述任何事物的类已经学习过的类工具类的使用规则练习实操类--ArrayUtilpack......
  • thinkphp漏洞
    thinkphp漏洞参考资料:https://github.com/SkyBlueEternal/thinkphp-RCE-POC-Collection敏感信息THINKPHP3.2结构:Application\Runtime\Logs\Home\16_09_09.logTHINKPHP......
  • 几个 wp-config.php 常量设置
    wp-config.php是WordPress一个最基本也是最重要的配置文件,你可以在这个文件中配置数据库信息,网站语言等等。今天介绍几个你可能不知道的wp-config.php设置选项。文章目......
  • php笔记
    php笔记参考资料https://zeo.cool/2020/12/31/webshell多种方法免杀/https://h3art3ars.github.io/2020/02/27/利用php新特性过静态查杀/函数记录<?phpphpinfo();<%......