php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #29174 __get and __set are not called when placed in a class that has a parent class
Submitted: 2004-07-15 03:50 UTC Modified: 2004-07-23 07:56 UTC
Votes:4
Avg. Score:4.0 ± 1.7
Reproduced:3 of 3 (100.0%)
Same Version:4 (133.3%)
Same OS:3 (100.0%)
From: jbeall at heraldic dot us Assigned:
Status: Not a bug Package: Scripting Engine problem
PHP Version: 5.0.0 OS: *
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: jbeall at heraldic dot us
New email:
PHP Version: OS:

 

 [2004-07-15 03:50 UTC] jbeall at heraldic dot us
Description:
------------
the __get() and __set() functions work fine to overload a class when that class has no parent class.  However, if the class you put __get() and __set() in has a parent class, they are not called whenever a property is called.

Reproduce code:
---------------
class Sub
{
}

class Super extends Sub
{
	function __get($prop)
	{
		echo "Property $prop called\n";
	}

	function __set($prop, $val)
	{
		echo "Property $prop set to $val\n";
	}
}


$foo = new Sub();

$foo->someProp = 10;
echo $foo->someProp;

Expected result:
----------------
Property someProp set to 10
Property someProp called

Actual result:
--------------
10

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2004-07-23 06:46 UTC] imprestavel at gameguru dot com dot br
i think extends works the other way around
"class Super extends Sub", means that Super will inherit Sub stuff, not the opposite
so, when you do:
> $foo = new Sub();
you are instantiating Sub, which doesn't inherit Super, ending up without the __get and __set methods

try:
<?
class Super
{
	function __get($prop)
	{
		echo "Property $prop called\n";
	}

	function __set($prop, $val)
	{
		echo "Property $prop set to $val\n";
	}
}

class Sub extends Super
{
}

$foo = new Sub();

$foo->someProp = 10;
echo $foo->someProp;
?>
 [2004-07-23 07:56 UTC] helly@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

Move get/set to base class.
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Wed Jul 16 23:01:33 2025 UTC