php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #47215 magic method __set() is bypassed on recursive call
Submitted: 2009-01-25 22:23 UTC Modified: 2009-01-26 00:09 UTC
Votes:1
Avg. Score:5.0 ± 0.0
Reproduced:1 of 1 (100.0%)
Same Version:1 (100.0%)
Same OS:1 (100.0%)
From: remy dot damour at laposte dot net Assigned:
Status: Not a bug Package: Class/Object related
PHP Version: 5.2.8 OS: debian
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: remy dot damour at laposte dot net
New email:
PHP Version: OS:

 

 [2009-01-25 22:23 UTC] remy dot damour at laposte dot net
Description:
------------
Magic method __set() is bypassed on recursive call. 
PHP automatically creates a property on instance instead of recursively calling __set() or instead of throwing a recursivity error

For additional details, please read: http://www.qc4blog.com/?p=458

Reproduce code:
---------------
class Fruits
{
	protected $_props = array(); // inner array mapping properties to values
	
	public function __set($name, $value) 
	{
		$this->_props[$name] = $value;
		if ('apple' == $name) {
			$this->apple = 'green';
		}
	}
}

$a = new Fruits();
$a->banana = 'yellow';
var_dump(property_exists($a, 'banana')); // output: false
$a->apple = 'red';
var_dump(property_exists($a, 'apple')); // output: true


Expected result:
----------------
Throw a recursivity exception

Actual result:
--------------
no exception thrown, property is automatically created by passing __set()

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2009-01-25 22:55 UTC] remy dot damour at laposte dot net
Maybe this is not a bug, but simply expected behavior?
Because otherwise we could not be able to define object properties using __set() magic method

Regards,
Remy
 [2009-01-26 00:09 UTC] 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

.
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Sun Jul 06 06:01:34 2025 UTC