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

From: Date: Mon, 20 Nov 2017 14:54:17 +0000
Subject: Bug #75543 [Com]: function "defined" should ignore class constant visibility
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-212641@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
 Comment by:         spam2 at rhsoft dot 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:

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');
_________________________________


Previous Comments:
------------------------------------------------------------------------
[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

------------------------------------------------------------------------
[2017-11-20 13:59:57] 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 13:55:38] matej21 at matej21 dot cz

Description:
------------
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:
---------------
<?php

class Foo
{
    private const BAR = 1;
}


var_dump(defined('Foo::BAR'));

Expected result:
----------------
bool(true)

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


------------------------------------------------------------------------



--
Edit this bug report at https://bugs.php.net/bug.php?id=75543&edit=1


Thread (10 messages)

« previous php.bugs (#212641) next »