|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2006-01-30 19:34 UTC] 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) {
}
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Tue Oct 28 11:00:01 2025 UTC |
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?Sure it doesn't return a reference, but why this example still prints "5.1.3-dev This shouldn't work, but why are you seeing this text?"? <?php class a { public $cond_oper = false, $array = array(); public function __set ($key, $value) { return $this->array[$key] = $value; } public function __get ($key) { if ($this->cond_oper) return true ? $this->array[$key] : null; else return $this->array[$key]; // This one works even though it shouldn't! } } $a = new a; $a->a = array(); $a->a[] = "This shouldn't work, but why are you seeing this text?"; $a->cond_oper = true; $a->a[] = "Nope, this text won't be displayed (which is ok)."; echo phpversion(), "\n", array_pop($a->a); ?> If I understood your explanations correctly, $a->a should've been an empty array and I should've seen two error messages telling me that __get method didn't return a reference etc. PHP snapshot I used was built on Feb 10, 2006 15:30 GMT.Is it the same issue? I suspect it to be the same but I'm unsure about the bogus state. I would like to have a little explanation, just to write a good work around if this is not a bug. In this example, the first init raises a notice "Indirect modification of overloaded property context::$attachments has no effect" as the second works like a charm. class context { public $stack = array(); public function __set($name,$var) { $this->stack[$name] = $var;return; } public function __get($name) { if (!isset($this->stack[$name])) { return null; } return $this->stack[$name]; } } $ctx = new context; $ctx->comment_preview = array(); $ctx->comment_preview['content'] = ''; $ctx->comment_preview['rawcontent'] = ''; $ctx->comment_preview['name'] = ''; $ctx->comment_preview['mail'] = ''; $ctx->comment_preview['site'] = ''; $ctx->comment_preview['preview'] = false; $ctx->comment_preview['remember'] = false; var_dump($ctx); $comment_preview = array(); $comment_preview['content'] = ''; $comment_preview['rawcontent'] = ''; $comment_preview['name'] = ''; $comment_preview['mail'] = ''; $comment_preview['site'] = ''; $comment_preview['preview'] = false; $comment_preview['remember'] = false; $ctx->comment_preview = $comment_preview; var_dump($ctx);