|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #75543 function "defined" should ignore class constant visibility
Submitted: 2017-11-20 13:55 UTC Modified: 2019-08-29 13:04 UTC
From: matej21 at matej21 dot cz Assigned: nikic (profile)
Status: Closed Package: Scripting Engine problem
PHP Version: 7.1.11 OS:
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
Block user comment
Status: Assign to:
Bug Type:
From: matej21 at matej21 dot cz
New email:
PHP Version: OS:


 [2017-11-20 13:55 UTC] matej21 at matej21 dot cz
When you check if private/protected constant is defined on a class, it currently triggers a fatal error. 
For a consistency with method_exists and property_exists it should probably ignore visibility and return true

Test script:

class Foo
    private const BAR = 1;


Expected result:

Actual result:
Fatal error: Uncaught Error: Cannot access private const Foo::BAR


Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2017-11-20 13:59 UTC] spam2 at rhsoft dot net
it should return false but not raise a fatal error at all, but there is no valid reason to return true because you can't access it outside the class
 [2017-11-20 14:47 UTC] matej21 at matej21 dot cz
The reason (as I said in the bug report) is a consistency with property_exists and method_exists, see
 [2017-11-20 14:54 UTC] spam2 at rhsoft dot net
you still compare different things here and for consistency they all should return false outside of the context $this because it's simply wrong behavior leading to errors like this one which couldn't work beause for the current scope it don't exist

if(property_exists($foo, 'bar')))
 $foo->bar = 10;

$foo = new Foo;
var_dump(property_exists($foo, 'bar'));
var_dump(method_exists($foo, 'bar'));

 [2017-11-20 15:06 UTC] matej21 at matej21 dot cz
I agree with you it would be better if a current scope were taken into account for all three methods.

But you can even see in a changelog of property_exists 
( ) that "This function checks the existence of a property independent of accessibility." So I suppose there was some good reason for this behavior and therefore I think "defined" function should behave in the same way.
 [2017-11-20 15:11 UTC]
I believe the closer analogon to defined() on constants is isset() on properties and is_callable() on methods, both of which will take visibility into account. property_exists() and method_exists() are more like reflection methods, from a time where reflection didn't exist yet.
 [2017-11-20 15:14 UTC] spam2 at rhsoft dot net but in no case it has to raise a exception / fatal error because such things are typically used to test if something exists instead blindly call it and then fail - so the current behavior defeats the whole purpose
 [2017-11-20 15:19 UTC]
Yes, obviously. My comment was referring to the discussion whether the return value should be true or false for an inaccessible class constant.
 [2017-11-20 17:42 UTC]
-Status: Open +Status: Verified
 [2019-08-29 13:04 UTC]
-Status: Verified +Status: Closed -Assigned To: +Assigned To: nikic
 [2019-08-29 13:04 UTC]
This has been fixed at some point:
PHP Copyright © 2001-2019 The PHP Group
All rights reserved.
Last updated: Mon Dec 16 08:01:27 2019 UTC