Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Dynamic associative array property

Status
Not open for further replies.

AdaHacker

Programmer
Sep 6, 2001
392
US
Greetings all!

I'm having a problem with trying to add elements to an array property accessed via a variable. The following dummy code illustrates the problem:
Code:
<?php
class foo { }
class bar {
	function bar() {
		$this->foo = new foo;
	}
	function setArrayValue($prop, $key, $val) {
		if (! isset($this->foo->$prop)) {
			$this->foo->$prop = array();
		}
		$this->foo->$prop[$key] = $val;
	}
}
$b = new bar;
$b->setArrayValue('Fizz', 'Buzz', 'Bazz');
$b->setArrayValue('abc', 'def', 'ghi');
var_dump($b);
?>
The idea is that the setArrayValue method will take a property name as a string ($prop) and create it as an empty array if it doesn't exists. Then it sets the $key element of that array to $val. However, that's not what actually happens. The above program prints out:
Code:
object(bar)(1) {
  ["foo"]=>
  object(foo)(4) {
    ["Fizz"]=>
    array(0) {
    }
    ["F"]=>
    string(4) "Bazz"
    ["abc"]=>
    array(0) {
    }
    ["a"]=>
    string(3) "ghi"
  }
}
As far as I can tell, PHP is treating the $prop[$key] portion of the array assignment as a string subscript and evaluating it as the first character of $prop. So instead of setting $b->foo->Fizz['Buzz'], it sets $b->foo->F.

Is there some syntax I don't know about that will force PHP to interpret $prop[$key] as an array element instead of a character in a string? Anyone know which, if any, section of the manual covers this? I can trivially work around the problem by using array_merge() instead assigning to an element, but that feels a little too ad hoc for my taste. It seems like there ought to be a better way to do it.
 
Why are you using a class instantiation inside a class? If your class "foo" has no methods, it should be an associative array, not a class. If class "foo" in reality has methods, I would use class inheritance.


Anyway, this version of your code:

Code:
<?php
class foo { }
class bar
{
    function bar()
    {
        $this->foo = new foo;
    }
    
    function setArrayValue($prop, $key, $val)
    {
        if (! isset($this->foo->prop))
        {
	        $this->foo->{$prop} = array();
        }
        
        $this->foo->{$prop}[$key] = $val;
    }
}
$b = new bar;
$b->setArrayValue('Fizz', 'Buzz', 'Bazz');
$b->setArrayValue('abc', 'def', 'ghi');
print '<pre>';
print_r($b);
?>

outputs:

[tt]bar Object
(
[foo] => foo Object
(
[Fizz] => Array
(
[Buzz] => Bazz
)

[abc] => Array
(
[def] => ghi
)

)

)[/tt]

Is this what you wanted? If so, read up on "Variable variables" in the PHP Online manual.



Want the best answers? Ask the best questions! TANSTAAFL!
 
Thank you, that's what I needed! I knew there had to be something like that, I just couldn't find it.

If your class "foo" has no methods, it should be an associative array, not a class. If class "foo" in reality has methods, I would use class inheritance.
Long story. In the real code, "foo" inherits from an abstract base class and "bar" is implementing another class's public interface, using a "foo" to facilitate the some of the grunt work. But none of that was relevant to the problem at hand, so I figured it would be shorter and less confusing to write some dummy code that displayed the same behavior.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top