#36214 [Opn->Asn]: Conditional operator fails to work properly when both __set and __get are set
| From: | tony2001@php.net | Date: | Fri, 03 Feb 2006 21:48:58 +0000 |
| Subject: | #36214 [Opn->Asn]: Conditional operator fails to work properly when both __set and __get are set | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-92881@lists.php.net to get a copy of this message | ||
ID: 36214
Updated by: tony2001@php.net
Reported By: pexu at lyseo dot edu dot ouka dot fi
-Status: Open
+Status: Assigned
Bug Type: Scripting Engine problem
Operating System: Windows XP
PHP Version: 5.1.2
-Assigned To:
+Assigned To: dmitry
Previous Comments:
------------------------------------------------------------------------
[2006-02-02 16:04:32] mike@php.net
That it works in the latter case is just a side affect which falls
under "undefined behaviour".
You should actually see an error telling you that __get() can't return
a reference or that array_push() wants a reference.
IIRC it's fixed in current CVS, could you please try?
Where "fixed" means that an error is generated.
Thanks.
------------------------------------------------------------------------
[2006-02-02 14:08:59] pexu at lyseo dot edu dot ouka dot fi
Even overload::$array is defined as public variable, actual result
remains the same. But if I change conditional operator to normal if ..
else clause, problem disappers!
So:
function __get ($key)
{
if (isset($this->array[$key]))
return $this->array[$key];
else
return null;
}
works just fine. So the actual problem can't be a private property
which is accessed outside the class, right?
------------------------------------------------------------------------
[2006-01-30 19:43:18] johannes@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
You're trying to acces a private property (arr) from
outside the class.
------------------------------------------------------------------------
[2006-01-30 19:34:55] pexu at lyseo dot edu dot ouka dot fi
Description:
------------
When both __set and __get are set, conditional operator "?" fails to
work when used to return variables from __get when working with
arrays.
A normal if .. else clause however works fine. With non-array variables
there's no problem, either.
If __set is not used, this bug doesn't seem to appear.
Reproduce code:
---------------
class overload
{
private $array = array();
public function __set($key, $value)
{
$this->array[$key] = $value;
}
public function __get($key)
{
return isset($this->array[$key])
? $this->array[$key]
: null;
}
}
$ol = new overload; $ol->arr = array();
array_push($ol->arr, "element");
var_dump($ol->arr);
Expected result:
----------------
array(1) {
[0]=>
string(7) "element"
}
Actual result:
--------------
array(0) {
}
------------------------------------------------------------------------
--
Edit this bug report at http://bugs.php.net/?id=36214&edit=1