#36214 [Bgs]: __get method works properly only when conditional operator is used

From: Date: Fri, 10 Feb 2006 16:42:38 +0000
Subject: #36214 [Bgs]: __get method works properly only when conditional operator is used
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-93132@lists.php.net to get a copy of this message
ID: 36214 User updated by: pexu at lyseo dot edu dot ouka dot fi -Summary: Conditional operator fails to work properly when both __set and __get are set Reported By: pexu at lyseo dot edu dot ouka dot fi Status: Bogus Bug Type: Scripting Engine problem Operating System: Windows XP -PHP Version: 5.1.2 +PHP Version: 5.1.3-dev Assigned To: dmitry New Comment: 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. Previous Comments: ------------------------------------------------------------------------ [2006-02-06 10:32:25] dmitry@php.net This is not a bug. Method __get() returns by value and it's result cannot be passed by reference. ------------------------------------------------------------------------ [2006-02-04 00:01:14] pexu at lyseo dot edu dot ouka dot fi I quickly tried the newest snapshot (20060203, 5.1.3-dev) but neither error messages were generated nor results were any different from 5.1.2. (Error_reporting was set to E_ALL | E_STRICT and display_errors was turned on. I tried both conditional operator and normal if clause.) But I guess I can utilize ArrayObject etc. to get the expected result I wanted. ------------------------------------------------------------------------ [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. ------------------------------------------------------------------------ The remainder of the comments for this report are too long. To view the rest of the comments, please view the bug report online at http://bugs.php.net/36214 -- Edit this bug report at http://bugs.php.net/?id=36214&edit=1

« previous php.bugs (#93132) next »