Bug #70791 [Com]: Inconsistent method_exists() property_exists() behavior.

From: Date: Sat, 31 Oct 2015 13:53:53 +0000
Subject: Bug #70791 [Com]: Inconsistent method_exists() property_exists() behavior.
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-196943@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=70791&edit=1

 ID:                 70791
 Comment by:         phplists at stanvassilev dot com
 Reported by:        phplists at stanvassilev dot com
 Summary:            Inconsistent method_exists() property_exists()
                     behavior.
 Status:             Not a bug
 Type:               Bug
 Package:            *General Issues
 Operating System:   OSX
 PHP Version:        7.0.0RC5
 Block user comment: N
 Private report:     N

 New Comment:

reeze@php.net:

Can you please check the code example again. This is not about respecting or ignoring visibility.

method_exists() returns true for a situation where the method simply doesn't exist for the
given class.

A private method in a superclass *does not exist* in the child class.


Previous Comments:
------------------------------------------------------------------------
[2015-10-30 15:43:41] reeze@php.net

I would say this is not a bug.  method_exists() did check for visibility. you could use
is_callable() instead.

------------------------------------------------------------------------
[2015-10-26 13:28:19] phplists at stanvassilev dot com

Description:
------------
The rules under which a class member is reported to "exist" for method_exists()
property_exists() are inconsistent.

property_exists will look into all members visible from inside a given class, be it private, public
or protected, but it won't report *parent class private properties* as existing for that class.

method_exists will report a parent class private method as existing for the child class, but the
child class has no access to it internally.

This is not a new problem in PHP7, but it's an issue in general. I believe the
property_exists() behavior is correct, and method_exists() causes unexpected results.

Test script:
---------------
class BobSenior {
	private $a;
	protected $b;
	
	private function a() {}
	protected function b() {}
}

class BobJunior extends BobSenior {
	private $c;
	protected $d;
	
	private function c() {}
	protected function d() {}
}

$senior = new BobSenior();
$junior = new BobJunior();

var_dump(property_exists($senior, 'a')); // bool(true) -> EXPECTED
var_dump(method_exists($senior, 'a')); // bool(true) -> EXPECTED

var_dump(property_exists($senior, 'b')); // bool(true) -> EXPECTED
var_dump(method_exists($senior, 'b')); // bool(true) -> EXPECTED

var_dump(property_exists($senior, 'c')); // bool(false) -> EXPECTED
var_dump(method_exists($senior, 'c')); // bool(false) -> EXPECTED

var_dump(property_exists($senior, 'd')); // bool(false) -> EXPECTED
var_dump(method_exists($senior, 'd')); // bool(false) -> EXPECTED

var_dump(property_exists($junior, 'a')); // bool(false) -> EXPECTED
var_dump(method_exists($junior, 'a')); // bool(true) -> *NOT* EXPECTED (EXPECTED =
false)

var_dump(property_exists($junior, 'b')); // bool(true) -> EXPECTED
var_dump(method_exists($junior, 'b')); // bool(true) -> EXPECTED

var_dump(property_exists($junior, 'c')); // bool(true) -> EXPECTED
var_dump(method_exists($junior, 'c')); // bool(true) -> EXPECTED

var_dump(property_exists($junior, 'd')); // bool(true) -> EXPECTED
var_dump(method_exists($junior, 'd')); // bool(true) -> EXPECTED

Expected result:
----------------
Please check comments for expected results.

Actual result:
--------------
Please check comments for actual results.


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



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


Thread (4 messages)

« previous php.bugs (#196943) next »