BC break: $o= new stdclass; empty($o)

php.internals

Timm Friebe

22 years ago
Hi, $ php5 -r '$o= new stdclass; var_dump(empty($o));' bool(false) $ php4 -r '$o= new stdclass; var_dump(empty($o));' bool(true) Was this an intentional change? - Timm

Timm Friebe

22 years ago
On Sun, 2004-02-29 at 18:10, Timm Friebe wrote:
> Hi,
[...]
> Was this an intentional change?
zend_execute.h, lines 95 - 98: case IS_OBJECT: /* OBJ-TBI */ result = 1; break; I have a patch, but that includes a TSRMLS_FETCH() - maybe it would be wise to change: ZEND_API int zend_is_true(zval *op); to ZEND_API int zend_is_true(zval *op TSRMLS_DC); but that breaks the API. - Timm

Stanislav Malyshev

22 years ago
TF>> I have a patch, but that includes a TSRMLS_FETCH() - maybe it would TF>> be wise to change: Patch that does what?
-- Stanislav Malyshev, Zend Products Engineer stas@zend.com http://www.zend.com/ +972-3-6139665 ext.109

Timm Friebe

22 years ago
On Sun, 2004-02-29 at 19:09, Stanislav Malyshev wrote:
> TF>> I have a patch, but that includes a TSRMLS_FETCH() - maybe it would > TF>> be wise to change: > > Patch that does what?
Well, fix the BC issue by implementing what was marked with "TBI" (which, as I assume, means "to be implemented"). When applied, both PHP4 and PHP5 behave the same: $ php5 -r '$o= new stdclass; var_dump(empty($o));' bool(true) $ php4 -r '$o= new stdclass; var_dump(empty($o));' bool(true) I just noticed there are more places (especially in zend_operators.c) where this needs to be done (grep for /* TBI!! */) - Timm

Andi Gutmans

22 years ago
Hey, The reason for this behavior is that in PHP 3 and 4, objects were treated very much like arrays (this is the main reason for redesigning the object model for PHP 5). I don't think that the old behavior is correct and an empty should always return true (i.e. empty($var) should return true if the variable is NULL and false if $var is an object). I don't think we should by default support the PHP 4 behavior because it's just not right. What I suggest is to either break BC completely or support the old behavior in compatibility mode. What option do you prefer? Andi At 07:03 PM 2/29/2004 +0100, Timm Friebe wrote:

Timm Friebe

22 years ago
On Thu, 2004-03-04 at 13:10, Andi Gutmans wrote:
> Hey,
Hi,
> The reason for this behavior is that in PHP 3 and 4, objects were treated > very much like arrays (this is the main reason for redesigning the object > model for PHP 5). > I don't think that the old behavior is correct and an empty should always > return true (i.e. empty($var) should return true if the variable is NULL > and false if $var is an object).
to clarify: Maybe I was misusing PHP by doing the following: class null { function null() { } function __call($name, $args) { throw(new NullPointerException('Method.invokation('.$name.')')); } function __set($name, $value) { throw(new NullPointerException('Property.write('.$name.')')); } function __get($name) { throw(new NullPointerException('Property.read('.$name.')')); } } class registry { public static $null; } registry::$null= new null(); class ClassType { function forName($name) { // ... abbreviated ... return new ClassType($name); } function getConstructor() { if (!$this->hasConstructor()) return registry::$null; return $this->constructor; } } $i= ClassType::forName($class)->getConstructor()->newInstance(); What I am doing here is fatal error prevention. To come to my point: The following statement: if (ClassType::forName($class)->getConstructor()) { // ... } would now always evaluate to (bool)true. In PHP4 (where, of course, the syntax need be a bit different) the above statement would evaluate to (bool)false due to the "emptiness" of the null object. I know this is kind of hacky but was a nice way of being able to have a way of implementing a "Fatal error: Call to a member function of a non-object"-safe "NULL" *and* being able to check for it using boolean operators.
> I don't think we should by default support the PHP 4 behavior because it's > just not right. What I suggest is to either break BC completely or support > the old behavior in compatibility mode. > What option do you prefer?
I saw you already committed a patch for the latter option (return 0 in compat mode). I'm not sure whether this affects a lot of users, and as noone has commented on it, I guess PHP5 will get away with this break:) I'll just have to find a workaround if you insist the PHP4 behaviour is broken:) - Timm

Timm Friebe

22 years ago
On Sat, 2004-03-06 at 15:53, Timm Friebe wrote: [...]
> I'm not sure whether this affects a lot of users, and as noone has > commented on it, I guess PHP5 will get away with this break:) > > I'll just have to find a workaround if you insist the PHP4 behaviour is > broken:)
Additionally, it should be mentioned in README.PHP4-TO-PHP5-THIN-CHANGES - Timm