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

From: Date: Mon, 20 Nov 2017 15:11: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-212643@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:

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.


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

------------------------------------------------------------------------
[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 (#212643) next »