php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #38636 overloading __set changes the behavior of property access to call __get
Submitted: 2006-08-29 01:03 UTC Modified: 2006-08-29 07:39 UTC
From: foobar at dodgeit dot com Assigned:
Status: Not a bug Package: Scripting Engine problem
PHP Version: 5.1.5 OS: FreeBSD 6.1
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: foobar at dodgeit dot com
New email:
PHP Version: OS:

 

 [2006-08-29 01:03 UTC] foobar at dodgeit dot com
Description:
------------
Consider the following code:

$foo = new A;
$foo->bar = 'bar';
echo $foo->bar;

Assume A defines __get.
Behavior of the last line is different depending on whether A defines __set. If __set is not defined, then the last line won't call __get. If __set is defined, __get will be called.

Reproduce code:
---------------
<?php
  class A { public function __get($prop) { echo "getting\n"; } }
  $a = new A; $a->foo = 'foo'; echo $a->foo."\n";
?>

------

<?php
  class A { public function __get($prop) { echo "getting\n"; } public function __set($prop, $val) { echo "setting\n"; } }
  $a = new A; $a->foo = 'foo'; echo $a->foo."\n";
?>


Expected result:
----------------
The first run produces:

foo

The second run produces:

getting
setting


Actual result:
--------------
Either 'getting' should be printed in both runs, or it should be not printed in either run.


Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2006-08-29 01:07 UTC] foobar at dodgeit dot com
Sorry, I reversed expected and actual results.
 [2006-08-29 07:39 UTC] tony2001@php.net
Expected behaviour.
Since there is no __set(), you created a "real" property with $a->foo = 'foo';, and __get() is called only for "virtual" properties.
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Sat Jul 05 04:01:35 2025 UTC