Bug #80075 [Opn->Nab]: property_exists will return true for a protected member from a third class
| From: | requinix@php.net | Date: | Tue, 08 Sep 2020 03:06:11 +0000 |
| Subject: | Bug #80075 [Opn->Nab]: property_exists will return true for a protected member from a third class | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-228936@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=80075&edit=1
ID: 80075
Updated by: requinix@php.net
Reported by: liuduan2004 at 163 dot com
Summary: property_exists will return true for a protected
member from a third class
-Status: Open
+Status: Not a bug
Type: Bug
Package: Class/Object related
Operating System: mac 10.15.6
PHP Version: 7.4.10
Block user comment: N
Private report: N
New Comment:
Shop *does* have a property $id_shop. It is protected and thus not accessible, but it still exists.
Consider using get_class_vars() or get_object_vars(), but I would suggest you reconsider your design
so that you don't need to care about property visibility.
Previous Comments:
------------------------------------------------------------------------
[2020-09-08 02:58:22] liuduan2004 at 163 dot com
it should be an issue , change bug type to bug.
------------------------------------------------------------------------
[2020-09-08 02:53:18] liuduan2004 at 163 dot com
Description:
------------
---
From manual page: https://php.net/function.property-exists
---
my php version is PHP 7.4.8 with mac env(10.15.6)
eg:
class Object {
protected id_shop;
}
class Shop extends Object{
}
vardump(property_exists(new Shop(), "id_shop")); // will return true,
it's not the same as array_key_exists.
when your code is like below, error occurs
Class EntityMapper {
public function setField($shop, $key, $value) {
if (property_exists($shop,$key)) {
$shop->{$key} = $value; // handles public fields
return;
}
}
}
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=80075&edit=1