Bug #75543 [Ver->Csd]: function "defined" should ignore class constant visibility
Edit report at https://bugs.php.net/bug.php?id=75543&edit=1
ID: 75543
Updated by: nikic@php.net
Reported by: matej21 at matej21 dot cz
Summary: function "defined" should ignore class constant
visibility
-Status: Verified
+Status: Closed
Type: Bug
Package: Scripting Engine problem
PHP Version: 7.1.11
-Assigned To:
+Assigned To: nikic
Block user comment: N
Private report: N
New Comment:
This has been fixed at some point: https://3v4l.org/qUMeI
Previous Comments:
------------------------------------------------------------------------
[2017-11-20 15:19:23] nikic@php.net
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 15:14:14] spam2 at rhsoft dot net
@nikic@php.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:11:22] nikic@php.net
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:06:06] 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
( http://cz2.php.net/manual/en/function.property-exists.php
) 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 14:54:15] 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'));
defined('Foo::BAR');
_________________________________
------------------------------------------------------------------------
The remainder of the comments for this report are too long. To view
the rest of the comments, please view the bug report online at
https://bugs.php.net/bug.php?id=75543
--
Edit this bug report at https://bugs.php.net/bug.php?id=75543&edit=1
Thread (10 messages)