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!

PHP + ArrayObject + __set overload

Status
Not open for further replies.

thenewa2x

Programmer
Dec 10, 2002
349
US
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.

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?

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top