php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #64871 The ::class operator should verify the class name
Submitted: 2013-05-17 16:48 UTC Modified: 2013-05-17 22:15 UTC
From: vrana@php.net Assigned: ralphschindler (profile)
Status: Wont fix Package: Class/Object related
PHP Version: 5.5.0RC1 OS: Any
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If this is not your bug, you can add a comment by following this link.
If this is your bug, but you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: vrana@php.net
New email:
PHP Version: OS:

 

 [2013-05-17 16:48 UTC] vrana@php.net
Description:
------------
One of the main advantages of the ::class operator could be that it verifies the class name upon creating the string instead of upon usage. It should trigger autoloading if the class doesn't exist.

I know that it's somewhat late to change this in PHP 5.5 but I was really surprised by the lack of this feature and about the fact that it wasn't considered in RFC.


Test script:
---------------
<?php
// I want to know about this typo.
echo stdClas::class;


Expected result:
----------------
Fatal error: Class 'stdClas' not found

Actual result:
--------------
stdClas

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2013-05-17 16:49 UTC] vrana@php.net
-Assigned To: +Assigned To: ralphschindler
 [2013-05-17 16:49 UTC] vrana@php.net
Ralph, what's your take on this?
 [2013-05-17 17:16 UTC] ralphschindler@php.net
I believe this came up, and I wouldn't support this.  This would be inconsistent with the current implementation of namespaces.  use Foo\Bar\Baz; for example, does not check the existence of Baz on "use".  It is not not till a type is used (either statically called or used to create a new instance of via new) that it will trigger autoloading (if there is one).

There are many places in PHP where type names are used, method signatures, and even in the following example:

$x = new stdClass;
var_dump($x instanceof FooBar);

PHP will not attempt to autoload FooBar to for the purposes of instanceof.

If you need to know that a particular type exists, class_exists(foo::class, true); should do what you need.

If you feel differently, I think you should bring this up on the internals mailing list.
 [2013-05-17 17:16 UTC] ralphschindler@php.net
-Status: Assigned +Status: Wont fix
 [2013-05-17 18:23 UTC] vrana@php.net
Makes sense, thanks for the explanation.
 [2013-05-17 18:34 UTC] David at grudl dot com
Ralph, totally agree. What do you think about fixing type hint "callable" to not 
trigger autoloading?
 [2013-05-17 20:13 UTC] ralphschindler@php.net
David,
I am not sure what behavior you're referencing.  When does callable trigger autoload?  Perhaps this is a conversation for the internals mailing list?
 [2013-05-17 22:15 UTC] vrana@php.net
Ralph, David is referring to https://bugs.php.net/61467. I agree that this is not the best place for the discussion about that.
 [2015-08-12 09:34 UTC] jan dot kahoun at heureka dot cz
I think, that this is not expected behaviour for many developers. You should documented it at http://php.net/manual/en/language.oop5.basic.php#language.oop5.basic.class.class
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Tue Apr 23 08:01:30 2024 UTC