php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #78053 empty() when testing $obj->var with magic __get function
Submitted: 2019-05-22 22:46 UTC Modified: 2019-05-23 00:02 UTC
From: bjorn dot macintosh at gmail dot com Assigned:
Status: Not a bug Package: *General Issues
PHP Version: 7.1.29 OS: Mac
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: bjorn dot macintosh at gmail dot com
New email:
PHP Version: OS:

 

 [2019-05-22 22:46 UTC] bjorn dot macintosh at gmail dot com
Description:
------------
php -v
PHP 7.1.23 (cli) (built: Feb 22 2019 22:08:13) ( NTS )
Copyright (c) 1997-2018 The PHP Group
Zend Engine v3.1.0, Copyright (c) 1998-2018 Zend Technologies

----

Created an object with a protected property

Created a magic get "__get($name) { return $this->name; }"

Assigned my variable a value within the class (in correct scope)

When testing if variable is empty outside of object scope it incorrectly returns true for emptyness


Test script:
---------------
```
var_dump($obj->var, empty($obj->var));
// Outputs:
// $obj->var = "something"
// empty($obj-<var) = true

But it will work if I pre-retrieve the variable.
$val = $obj->var
var_dump($var, empty($var));
// Outputs:
// $obj->var = "something"
// empty($obj-<var) = false

```

Expected result:
----------------
empty on protected variable with a getter should resolve the getter first, then test emptyness

Actual result:
--------------
empty call on a protected $obj->var - with a magic __get - returns true even if variable is not empty

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2019-05-22 22:51 UTC] requinix@php.net
-Status: Open +Status: Not a bug -Package: PHP Language Specification +Package: *General Issues
 [2019-05-22 22:51 UTC] requinix@php.net
isset() and empty() try to determine the existence of a property. That is separate from retrieving its value.

To provide custom support for retrieving property values you need __get. To provide custom support for determining the existence of properties you need __isset.
https://www.php.net/manual/en/language.oop5.overloading.php
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri Nov 01 01:01:28 2024 UTC