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 Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
If you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: liuduan2004 at 163 dot com
New email:
PHP Version: OS:

 

 [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

Pull Requests

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: Thu Dec 26 13:01:30 2024 UTC