未加星标

PHP: looking into usage of the ::class constant

字体大小 | |
[开发(php) 所属分类 开发(php) | 发布者 店小二05 | 时间 2017 | 作者 红领巾 ] 0人收藏点击收藏

G'day:

There's not much to this article. It's arisen from the fact there's a paucity of docs on this subject on thephp website, and they're quite hard to google for (given Google still insists on ignoring punctuation in searches, for some daftly ignorant reason. So searching for "php ::class" just gets one results for "php class". Not the same thing. Similarly seaching for "php ::class constant" doesn't help.

For the record, the two relevant docs pages are:

Class name resolution via ::class And the paragraph on another page that links to: ::class

I include those there as much for me to be able to easily re-find them as anything else.

In truth there's not much to this ::class construct. I just wish the docs were easier to locate. When discussing the feature, it's nice to be able to point to some docs. Or now: a blog article ;-)

So what does it do?

All classes have a built-in constant ( ::class ) that contains a string that is the class's name. Same as what the get_class function will return for an object, but it works on the actual class too (because it's a class constant). Here are some examples:

<?php namespace me\adamcameron\cc; use com\example\other\SomeClassToAlias as AliasedClass; use com\example\other\SomeClassToAlias; use com\example\other\SomeOtherClass; require_once realpath(__DIR__ . '/../vendor/autoload.php'); echo "Using ::class" . PHP_EOL; printf("SomeClass: %s%s", SomeClass::class, PHP_EOL); printf("SomeOtherClass: %s%s", SomeOtherClass::class, PHP_EOL); printf("SomeClassToAlias as AliasedClass: %s%s", AliasedClass::class, PHP_EOL); printf("SomeClassToAlias: %s%s", SomeClassToAlias::class, PHP_EOL); printf("PHPUnit_Framework_Exception: %s%s", \PHPUnit_Framework_Exception::class, PHP_EOL); $someClass = new SomeClass(); $someOtherClass = new SomeOtherClass(); $someAliasedClass = new AliasedClass(); $someClassToAlias = new SomeClassToAlias(); $phpunitFrameworkException = new \PHPUnit_Framework_Exception(); echo PHP_EOL . "Using get_class() on instance" . PHP_EOL; printf("SomeClass: %s%s", get_class($someClass), PHP_EOL); printf("SomeOtherClass: %s%s", get_class($someOtherClass), PHP_EOL); printf("AliasedClass: %s%s", get_class($someAliasedClass), PHP_EOL); printf("SomeClassToAlias: %s%s", get_class($someClassToAlias), PHP_EOL); printf("PHPUnit_Framework_Exception: %s%s", get_class($phpunitFrameworkException), PHP_EOL);

In the example all the classes in the me\adamcameron or com\example namespaces are just empty classes, eg:

<?php namespace me\adamcameron\cc; class SomeClass {}

I've also included a PHPUnit class there as PHPUnit is a lib I'm aware of that doesn't use namespaces (dunno why), and wanted to see how that behaved, in case there were quirks: seemingly not.

Here's the output:

C:\src\php\php.local\src\oo\classConstant\src>php testWithNamespace.php

Using ::class

SomeClass: me\adamcameron\cc\SomeClass

SomeOtherClass: com\example\other\SomeOtherClass

SomeClassToAlias as AliasedClass: com\example\other\SomeClassToAlias

SomeClassToAlias: com\example\other\SomeClassToAlias

PHPUnit_Framework_Exception: PHPUnit_Framework_Exception

Using get_class() on instance

SomeClass: me\adamcameron\cc\SomeClass

SomeOtherClass: com\example\other\SomeOtherClass

AliasedClass: com\example\other\SomeClassToAlias

SomeClassToAlias: com\example\other\SomeClassToAlias

PHPUnit_Framework_Exception: PHPUnit_Framework_Exception

C:\src\php\php.local\src\oo\classConstant\src>

So you see there are no surprises really: ::class just returns the fully-qualified path of the class concerned. One thing to not is that it does not pay any attention to aliasing , whether on the class itself or on an object of that aliased class . I guess an alias is just for clarity in source code, rather than any sort of "renaming" exercise.

Where does one use ::class constants?

Well my primary usage is when mocking, using PHPUnit. Instead of this:

$mockedThing = $this->getMockBuilder('path\to\class\being\mocked\MockThisClass') ->disableOriginalConstructor() ->setMethod(['someMethod']) ->getMock();

We just have this:

$mockedThing = $this->getMockBuilder(MockThisClass::class) ->disableOriginalConstructor() ->setMethod(['someMethod']) ->getMock();

(and PHPStorm even includes the use statement for me, automatically):

use path\to\class\being\mocked\MockThisClass;

It's good to keep all the class pathing references together at the top of the file.

One flaw in this constant is this behaviour:

<?php require_once realpath(__DIR__ . '/../vendor/autoload.php'); echo "Using ::class" . PHP_EOL; printf("SomeNonExistentClass: %s%s", SomeNonExistentClass::class, PHP_EOL);

Any sensible person would probably want an error to be thrown there. But... no. PHP does this:

C:\src\php\php.local\src\oo\classConstant\src>php testWithNonExistentClass.php

Using ::class

SomeNonExistentClass: SomeNonExistentClass

C:\src\php\php.local\src\oo\classConstant\src>

Groan. Why does PHP insist on being "helpful" like this. The class doesn't exist! Just say that . FFS.

Oh well... that sort of thing is almost to be expected of PHP, I guess. Sigh. Well this ::class constant mostly a well-implemented, if not glamourous, small feature in PHP. And now I seem to have documented ::class more than PHP itself has. Heh.

Speaking of documentation, I can't help but think this ::class constant should also be mentioned on a coupla other pages in the PHP docs:

Class constants Predefined constants

It'd be a good fit for both of those pages, plus that's where Google lands you if you search for it.

That's it. I have to dash to go visit my son for a few hours. Thanks to my colleague Carlos for pointing this whole thing out to me, btw. Both the ::class constant construct itself, but also the observation to be made about aliases. Nice one, fella.

Righto.

--

Adam

PS: apologies for the blatant SEO keyword stuffing of ::class constant in this article ("oops... I did it again"). It was by design.

本文开发(php)相关术语:php代码审计工具 php开发工程师 移动开发者大会 移动互联网开发 web开发工程师 软件开发流程 软件开发工程师

主题: PHPSEO
分页:12
转载请注明
本文标题:PHP: looking into usage of the ::class constant
本站链接:http://www.codesec.net/view/522130.html
分享请点击:


1.凡CodeSecTeam转载的文章,均出自其它媒体或其他官网介绍,目的在于传递更多的信息,并不代表本站赞同其观点和其真实性负责;
2.转载的文章仅代表原创作者观点,与本站无关。其原创性以及文中陈述文字和内容未经本站证实,本站对该文以及其中全部或者部分内容、文字的真实性、完整性、及时性,不作出任何保证或承若;
3.如本站转载稿涉及版权等问题,请作者及时联系本站,我们会及时处理。
登录后可拥有收藏文章、关注作者等权限...
技术大类 技术大类 | 开发(php) | 评论(0) | 阅读(52)