php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #38507 __set "overloading" and language incongruency with arrays
Submitted: 2006-08-19 00:26 UTC Modified: 2006-08-21 08:33 UTC
Votes:1
Avg. Score:1.0 ± 0.0
Reproduced:0 of 1 (0.0%)
From: phpa dot 20 dot crgtk at spamgourmet dot com Assigned:
Status: Not a bug Package: Scripting Engine problem
PHP Version: 5CVS-2006-08-18 (snap) OS: Windows XP/2003 but not relevant
Private report: No CVE-ID: None
 [2006-08-19 00:26 UTC] phpa dot 20 dot crgtk at spamgourmet dot com
Description:
------------
Fact: By utilizing the __get() overload functionality in a class, PHP allows one to return a dynamic member of an object as an array that can be used as if the array were an actual static member of the object. [Example (1) below].

Issue: However, a glaring incongruency in the PHP language arrises when one attempts to utilze the __set() overload functionality to modify the given dynamic member array. One can get, but cannot set the dynamic member as if it were an array. [Example (2) below].

Consequence: This situation prevents one from making a drop-in replacement object -- that utilizes dynamic member arrays -- for an object that originally contained static member arrays.

Reproduce code:
---------------
class MyClass
{
	function __get($name){
		return array("hooray"=>"for", "language"=>"incongruencies");
	}
	function __set($name, $value){
		echo "set was passed:\n";
		echo print_r($name, true)."\n";
		echo print_r($value, true)."\n";
	}
}

$instance = new MyClass();

// (1) proof of dynamic member "being" an array
print_r($instance->dynmember); 

// (1) proof of dynamic member array "being accessed"
$piece = $instance->dynmember["language"]; 
echo "piece is: $piece\n\n";

// (2) issue: since one can do the above, one expects to be able to do the following
$piece = "being congruent";
$instance->dynmember['language'] = $piece; // as of php 5.2, literally does nothing

Expected result:
----------------
Since the language does not seem to have any current capability of detecting this condition, the following is a _proposed_ result of the above code:

Array
(
    [hooray] => for
    [language] => incongruencies
)
piece is: incongruencies

set was passed:
dynmember
Array
(
    [language] => being congruent
)





Actual result:
--------------
Please observe that there are no errors in the output below. That is because there are literally no errors raised by PHP; the code fails silently.

Array
(
    [hooray] => for
    [language] => incongruencies
)
piece is: incongruencies


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2006-08-20 09:41 UTC] ruslan dot kyrychuk at gmail dot com
$instance->dynmember['language'] = $piece;
will get array by __get overload and than set array value in memory. This is simple and clear logic.
With your expected result you must override __set property additionaly for arrays and simple objects.
 [2006-08-21 08:33 UTC] tony2001@php.net
__get() returns a value, not a reference, so you can't change the result of __get() and expect the object's attribute to change also.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sat Apr 27 16:01:29 2024 UTC