您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

对PHPstatic的理解

2024/6/30 1:37:47发布24次查看
最近看了下yii2.0的源码,发现里面有很多new static() 和static::的语句,顺便做下记录
- 首先看下面代码
classa {publicstaticfunctionget_self() {returnnewself(); } publicstaticfunctionget_static() {returnnewstatic(); }}classbextendsa {}echo get_class(b::get_self()); // aecho get_class(b::get_static()); // becho get_class(a::get_static()); // a
通过上面可以看出new static() 表示的调用的类,而new self() 表示的是声明的类
classa {const test = 'testa'; publicstaticfunctiontest_static(){echostatic::test; } publicstaticfunctiontest_self() {echoself::test; }}classbextendsa {const test = 'testb';}echo b::test_static(); //testbecho b::test_self(); //testa
通过上面的观察可以得出一个结论,static和被调用的类相关,而self则和声明的类(self代码所在的类)相关。
以上就介绍了对phpstatic的理解,包括了方面的内容,希望对php教程有兴趣的朋友有所帮助。
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product