At the moment I can set and retrieve array values using the ArrayObject class when overloading my class using __get. However for some reason the __set function isn't triggered when setting a value but the value is set anyway, in the appropriate private property.
Is this because the [tt]$myarray[/tt] object is being returned as a reference? If so, how can I get around this? Should I implement my own class extending ArrayObject?
Code:
<?php
$test = new MyClass ( );
$test->VAR1['test'] = "Should be preceded by a message.";
print( $test->VAR1['test'] ); // Prints value but doesn't trigger __set.
class MyClass
{
private $myarray = null;
private $myarray2 = null;
public function __construct ( )
{
$this->myarray = new ArrayObject ( );
$this->myarray2 = new ArrayObject ( );
}
public function __get ( $property )
{
switch ( $property )
{
case "VAR1":
return $this->myarray;
case "VAR2":
return $this->myarray2;
}
return( null );
}
public function __set ( $property, $value )
{
switch ( $property )
{
case "VAR1":
print( "Setting VAR1." );
array_merge( $this->myarray,
$value );
break;
case "VAR2":
print( "Setting VAR2." );
array_merge( $this->myarray2,
$value );
break;
}
}
}
?>
Is this because the [tt]$myarray[/tt] object is being returned as a reference? If so, how can I get around this? Should I implement my own class extending ArrayObject?