Re: __get() "feature"

From: Date: Thu, 11 Nov 2004 19:33:02 +0000
Subject: Re: __get() "feature"
References: 1 2  Groups: php.internals 
Request: Send a blank email to internals+get-13825@lists.php.net to get a copy of this message
On Nov 11, 2004, at 1:17 PM, Marcus Boerger wrote:
<?
class sales_order {
     ...
public function has_a( $class ) {
     $this->relationships[ $class ] = 'has_a';
}
public function __get( $class ) {
     if(array_key_exists( $class, $this->relationships )) {
       if( $this->relationships[ $class ] == 'has_a') {
         $this->$class  = new $class( $this->id );
         return;
Change the above line to:
          return $this->$class;
I'd tried that initially as: public function __get( $class ) { if(array_key_exists( $class, $this->relationships )) {
    if( $this->relationships[ $class ] == 'has_a') {
      $this->$class  = new $class( $this->id );
      return $this->$class;
    }
} } called from: $sales_order = new sales_order( 1 ); $sales_order->has_a( 'customer' ); print $sales_order->customer->address1; results in no output being generated. Using a suggestion of Todd Ruth's: public function __get( $class ) { if(array_key_exists( $class, $this->relationships )) {
    if( $this->relationships[ $class ] == 'has_a') {
      $value = $this->$class  = new $class( $this->id );
      return $value;
    }
} } and then running that same print code from above results in: 123 maple street being generated. Which is the test data that I have for the customer table associated with the sales_order with primary id of 1. Weird, huh? -- Jason N Perkins

« previous php.internals (#13825) next »