Bug #75543 [Opn]: function "defined" should ignore class constant visibility

From: Date: Mon, 20 Nov 2017 15:19:24 +0000
Subject: Bug #75543 [Opn]: function "defined" should ignore class constant visibility
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-212645@lists.php.net to get a copy of this message
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: Open Type: Bug Package: Scripting Engine problem PHP Version: 7.1.11 Block user comment: N Private report: N New Comment: Yes, obviously. My comment was referring to the discussion whether the return value should be true or false for an inaccessible class constant. Previous Comments: ------------------------------------------------------------------------ [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'); _________________________________ ------------------------------------------------------------------------ [2017-11-20 14:47:08] matej21 at matej21 dot cz The reason (as I said in the bug report) is a consistency with property_exists and method_exists, see https://3v4l.org/7PYOQ ------------------------------------------------------------------------ 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

« previous php.bugs (#212645) next »