php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #79462 method_exists and property_exists incoherent behavior
Submitted: 2020-04-09 01:02 UTC Modified: 2020-04-09 16:44 UTC
From: lucasfbustamante at gmail dot com Assigned: girgias (profile)
Status: Closed Package: Class/Object related
PHP Version: 7.4.4 OS: Linux
Private report: No CVE-ID: None
 [2020-04-09 01:02 UTC] lucasfbustamante at gmail dot com
Description:
------------
This output speaks more than a thousand words: https://3v4l.org/oRnjq

The first argument of method_exists just returns false if it's not an object, while it's twin method, property_exists, shows a warning if the exact same thing happens.

This behavior is inconsistent and prone to cause confusion amongst users.

I'd like to suggest that `property_exists` just returns false if the first argument is not an object, like `method_exists` does.

Best regards,
Lucas

Test script:
---------------
<?php

var_dump( method_exists(null, 'foo') );

var_dump( property_exists(null, 'foo') );

Expected result:
----------------
`property_exists` returns false if the first argument is not an object

Actual result:
--------------
`property_exists` throws a warning if the first argument is not an object

Patches

Add a Patch

Pull Requests

Pull requests:

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2020-04-09 02:46 UTC] requinix@php.net
-Package: Compile Warning +Package: Class/Object related
 [2020-04-09 08:04 UTC] nikic@php.net
-Assigned To: +Assigned To: girgias
 [2020-04-09 08:04 UTC] nikic@php.net
We probably should change method_exists() to throw TypeError, just like property_exists() does now.
 [2020-04-09 09:20 UTC] cmb@php.net
The following pull request has been associated:

Patch Name: Fix #79462: method_exists and property_exists incoherent behavior
On GitHub:  https://github.com/php/php-src/pull/5362
Patch:      https://github.com/php/php-src/pull/5362.patch
 [2020-04-09 09:38 UTC] cmb@php.net
Automatic comment on behalf of cmbecker69@gmx.de
Revision: http://git.php.net/?p=php-src.git;a=commit;h=e6458d67cfa54cdcd6d3c96897106fa8cb738414
Log: Fix #79462: method_exists and property_exists incoherent behavior
 [2020-04-09 09:38 UTC] cmb@php.net
-Status: Assigned +Status: Closed
 [2020-04-09 16:44 UTC] lucasfbustamante at gmail dot com
Thanks for solving this so quickly. Even though the outcome is not what I expected, I understand. Thank you. Have a good one!
 
PHP Copyright © 2001-2021 The PHP Group
All rights reserved.
Last updated: Mon Jan 18 08:01:26 2021 UTC