getcalledclass php,php – 差异static :: class vs get_called_class()和__CLASS__ vs get_class()vs self :: ...

之间的差异

returns the name of the class of an object

当您将对象实例指针作为第一个也是唯一的参数传递时,它返回一个类名,包括当前类的限定名称空间(不带参数)或任何指定的对象实例.

返回限定名称空间和当前类名的魔术常量.在这里,您无法测试其他对象的类名.

根据PHP 5.4,它适用于特征.也就是说,当在类中使用特征时,它将返回该类的名称空间和名称.

仅自PHP 5.5起可用.它使用类名和命名空间解析来获取信息,因此它不需要事先实例化类.另请注意:

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.

测试

namespace nTest;

trait tTest {

function __toString() {return get_class();}

function className() {return __CLASS__;} // per PHP 5.4

function traitName() {return __TRAIT__;}

}

class cTest {

use tTest;

function usedTraitName() {return __TRAIT__;}

}

class cClassWithoutObject {}

$oTest = new cTest;

header('Content-type: text/plain');

print // Output:

$oTest . PHP_EOL // 'nTest::cTest'

. get_class($oTest) . PHP_EOL // 'nTest::cTest'

. $oTest->className() . PHP_EOL // 'nTest::cTest'

. $oTest->traitName() . PHP_EOL // 'nTest::tTest' (trait!)

. $oTest->usedTraitName() . PHP_EOL // '' (no trait!)

. cTest::class . PHP_EOL // 'nTest::cTest'

. cClassWithoutObject::class; // 'nTest::cTestNotInstantiated'

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值