php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #29175 __get and __set are not called when property explicitly defined public or var
Submitted: 2004-07-15 03:56 UTC Modified: 2004-07-15 17:51 UTC
From: jbeall at heraldic dot us Assigned:
Status: Not a bug Package: Scripting Engine problem
PHP Version: 5.0.0 OS: Linux
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:56 UTC] jbeall at heraldic dot us
Description:
------------
If you define a property as public, and then also define the __get() and __set() functions within the same class, those functions are not called.

Reproduce code:
---------------
class Sub
{
	public $someProp;
	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-15 04:20 UTC] amt@php.net
This is the intended behavior. __get() and __set() are 
only triggered when a property *does not* exist.
 [2004-07-15 14:33 UTC] jbeall at heraldic dot us
Interesting.  The documentation suggests otherwise, both here: http://us2.php.net/manual/en/migration5.oop.php#migration5.oop.overload and here: http://us4.php.net/overload

It would probably be a good idea to update the docs so they make it clear what the intended behavior is.
 [2004-07-15 17:51 UTC] amt@php.net
I updated the manual's XML source to mention this in the PHP 5 Migration section.

The overload section, as already marked in the manual, is for PHP 4 only and not PHP 5.
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Sat Jul 12 05:01:33 2025 UTC