php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #80075 property_exists will return true for a protected member from a third class
Submitted: 2020-09-08 02:53 UTC Modified: 2020-09-08 03:06 UTC
From: liuduan2004 at 163 dot com Assigned:
Status: Not a bug Package: Class/Object related
PHP Version: 7.4.10 OS: mac 10.15.6
Private report: No CVE-ID: None
View Add Comment Developer Edit
Anyone can comment on a bug. Have a simpler test case? Does it work for you on a different platform? Let us know!
Just going to say 'Me too!'? Don't clutter the database with that please !
Your email address:
MUST BE VALID
Solve the problem:
30 + 43 = ?
Subscribe to this entry?

 
 [2020-09-08 02:53 UTC] 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;
      }
   }
}


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2020-09-08 02:58 UTC] liuduan2004 at 163 dot com
-Type: Documentation Problem +Type: Bug
 [2020-09-08 02:58 UTC] liuduan2004 at 163 dot com
it should be an issue , change bug type to bug.
 [2020-09-08 03:06 UTC] requinix@php.net
-Status: Open +Status: Not a bug
 [2020-09-08 03:06 UTC] requinix@php.net
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.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri Apr 26 12:01:30 2024 UTC