php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #57013 Unary operator __bool doesn't work
Submitted: 2006-05-14 13:33 UTC Modified: 2006-05-15 16:31 UTC
From: cbelin at free dot fr Assigned: pollita (profile)
Status: Closed Package: operator (PECL)
PHP Version: 5.1.4 OS: Windows XP SP2
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: cbelin at free dot fr
New email:
PHP Version: OS:

 

 [2006-05-14 13:33 UTC] cbelin at free dot fr
Description:
------------
I want to use objects for primary types like integers and boolean. So, this the basic implementation for the BooleanObject class :

<?php
class BooleanObject // implements IConvertible
{
	private $value;
	
	public function __construct($value)
	{
		$this->value=(bool) $value;
	}
	
	public function __bool()
	{
		return $this->ToBooleanType();
	}
	
	public function __bool_not()
	{
		return !$this->ToBooleanType();
	}
	
	// member of IConvertible interface
	public function ToBooleanType()
	{
		return $this->value;
	}
}
?>

I use ToBooleanType() method because of the IConvertible interface which defines m?thods for converting objects to primary types.

I use ToBooleanType

Reproduce code:
---------------
<?php
// tests passes : $boolean evaluate to TRUE
$boolean=new BooleanObject(true);
if($boolean) echo 'Test 1 : passed.';
else echo 'Test 1 : failed.';
echo PHP_EOL;

// test fails : $boolean evaluate to TRUE instead of FALSE
$boolean=new BooleanObject(false);
if($boolean) echo 'Test 2 : failed.';
else echo 'Test 2 : passed.';
echo PHP_EOL;
?>

Expected result:
----------------
Test 1 : passed.
Test 2 : failed.

Actual result:
--------------
Test 1 : passed.
Test 2 : passed.

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2006-05-14 13:37 UTC] cbelin at free dot fr
Sorry, wrong PHP version : test was run on PHP 5.1.4 Win32.
 [2006-05-15 12:47 UTC] cbelin at free dot fr
Oops ! Sorry, I was a bit asleep when I wrote the previous messages ;)

The results are (not as in the first message) :

Expected result:
----------------
Test 1 : passed.
Test 2 : passed.

Actual result:
--------------
Test 1 : passed.
Test 2 : failed.
 [2006-05-15 16:31 UTC] pollita@php.net
You won't like the answer but.....

I made a mistake putting the code covering ZEND_BOOL and ZEND_BOOL_NOT into the package.  They snuck in with some other commit and shouldn't have been part of any release. (Which is also why there was no regression test for it)

Bool/Bool_Not the way you want to use them are part of the JMPZ JMPNZ ops which can't be cleanly overloaded without causing issues in switch/for/while and other constructs.  At best I could put in support for (cast) expressions allowing stuff like:   if ((bool)$obj) {}, but that's not quite the same thing...
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sat Dec 21 17:01:58 2024 UTC