Yii2 中 ::class 与 ::className() 的区别?

编程入门 行业动态 更新时间:2024-10-11 01:09:52
本文介绍了Yii2 中 ::class 与 ::className() 的区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送ChatGPT账号..

我知道两者都给出了相同的结果,但我很期待听到有关性能的消息.

i know both gives same results but i am looking forward to hear about performance.

我不确定,但我认为..

i am not sure but i think..

"MyClass::className()" 也导致该类文件被加载(它的 Yii 函数的主体只是 get_called_class())

"MyClass::className()" cause that class file to be loaded as well (its Yii function whom body is just simply get_called_class())

"MyClass::class" 我认为这个 php 的原生类属性不会加载类 php 文件,只是根据当前命名空间或 use.

"MyClass::class" i think this php's native class property don't load class php file and just return its name based on current namespace or use.

让我知道我是否正确?或请突出您的知识.

Let me know if i am right? or highlight your knowledge please.

有很多地方我们只想要完整的限定类名称,即使当时不会使用它.但我也不喜欢放置硬编码字符串(由于硬重构)

There are many places where we just want full qualitfied class name even it's not going to be used that time. but i also don't like putting hardcoded strings (due to hard refactoring)

推荐答案

是的,我刚刚发现我是对的.

Yes i just found i was right.

PHP 的原生类属性可以节省性能..(PHP 5.5+)

PHP's native class property is performance saving..(PHP 5.5+)

见这里http://php/manual/en/language.oop5.basic.php#language.oop5.basic.class.class.name

所以这不会导致类文件被加载,所以即使类不存在它也会返回完整的类名

so this don't cause class file to be loaded, so even if class doesn't exists it will return full className

注意:使用::class的类名解析是编译时转型.这意味着当时类名字符串是created 尚未发生自动加载.因此,类名即使该类不存在,也会被扩展.没有错误发出那种情况.

Note: The class name resolution using ::class is a compile time transformation. That means at the time the class name string is created no autoloading has happened yet. As a consequence, class names are expanded even if the class does not exist. No error is issued in that case.

这篇关于Yii2 中 ::class 与 ::className() 的区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

更多推荐

[db:关键词]

本文发布于:2023-04-23 19:05:15,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1047454.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:区别   class   className

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!