php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #76710 result of empty() is not correct
Submitted: 2018-08-06 08:50 UTC Modified: 2018-08-06 08:56 UTC
Votes:1
Avg. Score:3.0 ± 0.0
Reproduced:1 of 1 (100.0%)
Same Version:1 (100.0%)
Same OS:1 (100.0%)
From: mehdimn dot mail at gmail dot com Assigned:
Status: Not a bug Package: Variables related
PHP Version: 7.2.8 OS: linux ubuntu
Private report: No CVE-ID: None
 [2018-08-06 08:50 UTC] mehdimn dot mail at gmail dot com
Description:
------------
The result of empty($objA->g) is TRUE but result of var_dump or echo the $objA->g is a string value.

Maybe help you to fix it, I found : https://bugs.php.net/bug.php?id=36786

Test script:
---------------
<?php
# test empty result
class A {
	private $g = '';

	public function __get($property) {
		return $this->$property;
	}
	
	function run() {
		$this->g = '4, 5, 6';
	}

	function callA() {
		$clsB = new B();
		$this->run();
		$clsB->callB($this);
	}
}

class B {
	function callB($objA) {
		echo '<pre>';
		var_dump($objA->g); // is not empty
		var_dump(empty($objA->g)); // result is TRUE !? 
		echo '</pre>';
	}
}

$a = new A();
$a->callA();
?>

Expected result:
----------------
FALSE

Actual result:
--------------
TRUE

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2018-08-06 08:56 UTC] requinix@php.net
-Status: Open +Status: Not a bug
 [2018-08-06 08:56 UTC] requinix@php.net
Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at
http://www.php.net/manual/ and the instructions on how to report
a bug at http://bugs.php.net/how-to-report.php

http://php.net/manual/en/function.empty.php
> Note:
> When using empty() on inaccessible object properties, the __isset() overloading method will be called, if
> declared.

You need to implement __isset.

In general, __get, __set, and __isset must all be implemented together for properties to work correctly.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri Mar 29 13:01:29 2024 UTC