php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #51888 overloaded __set is called from parent class
Submitted: 2010-05-22 17:17 UTC Modified: 2010-05-25 09:08 UTC
From: j dot jeising at gmail dot com Assigned:
Status: Not a bug Package: Class/Object related
PHP Version: 5.3.2 OS: Mac OS X
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: j dot jeising at gmail dot com
New email:
PHP Version: OS:

 

 [2010-05-22 17:17 UTC] j dot jeising at gmail dot com
Description:
------------
If you overwrite __set in a child class, the parent implementation is called first 
and than triggers the child version.

I found another bug, which says that you should implement the __set method in the 
parent class, but it in this case both methods are called.

A similar bug is reported in #44807, but it's only about the recursion prevention.

Test script:
---------------
<?php
	class Foo {
		function __set($key, $value) {
			$this->{$key} = $value . ' (Foo)';
		}
	}
	
	class Bar extends Foo {
		function __set($key, $value) {
			$this->{$key} = $value . ' (Bar)';
		}
		
		function set() {
			parent::__set('foobar', 'Test');
		}
	}
	
	$bar = new Bar();
	$bar->set();
	echo $bar->foobar;
?>

Expected result:
----------------
Test (Foo)

Actual result:
--------------
Test (Foo) (Bar)

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2010-05-25 09:08 UTC] mike@php.net
-Status: Open +Status: Bogus
 [2010-05-25 09:08 UTC] mike@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 call parent::__set() which assigns an unkown property, which in turn calls the __set() of the child.

Jeeeeez
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Wed Jul 02 11:01:36 2025 UTC