Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:13823 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 62517 invoked by uid 1010); 11 Nov 2004 15:21:27 -0000 Delivered-To: ezmlm-scan-internals@lists.php.net Delivered-To: ezmlm-internals@lists.php.net Received: (qmail 62492 invoked from network); 11 Nov 2004 15:21:27 -0000 Received: from unknown (HELO luke.segpub.com.au) (64.49.254.53) by pb1.pair.com with SMTP; 11 Nov 2004 15:21:27 -0000 Received: (qmail 18424 invoked from network); 11 Nov 2004 15:21:26 -0000 Received: from 64.202.110.58 by luke.segpub.com.au (envelope-from , uid 82) with qmail-scanner-1.23 (clamdscan: 0.75.1. spamassassin: 2.64. Clear:RC:1(64.202.110.58):. Processed in 0.033075 secs); 11 Nov 2004 15:21:26 -0000 Received: from unknown (HELO ?192.168.2.125?) (64.202.110.58) by 0 with SMTP; 11 Nov 2004 15:21:26 -0000 Mime-Version: 1.0 (Apple Message framework v619) Content-Transfer-Encoding: 7bit Message-ID: <5917D4B9-33F5-11D9-8B36-000D932DF2B4@sneer.org> Content-Type: text/plain; charset=US-ASCII; format=flowed To: internals@lists.php.net Date: Thu, 11 Nov 2004 09:21:23 -0600 X-Mailer: Apple Mail (2.619) Subject: __get() "feature" From: jperkins@sneer.org (Jason Perkins) I've ran into an issue using __get() and want to ascertain if this is how it was intended to work or if it's a bug. The general notion of what I'm attempting to accomplish using __get() is that a given object will potentially contain other objects, but these contained objects aren't known (or generated) until runtime. For example, we'll instantiate a sales_order object and then, using its has_a() method, add a customer object to it. To prevent cascades of objects being instantiated, I'd plan to use lazy initialization - the customer object won't be instantiated until the first time that it's accessed through the customer class. The problem that I'm having is that lazy initialization in PHP 5 is accomplished via the __get() method which is called when an attribute (or contained class in this) isn't available - it's passed the name of the property that wasn't found and that's not enough information for me to return the attribute of the contained object that was being accessed. Here's some quickly written code with extra code stripped that'll demonstrate what I mean: 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; } } } } So if $attribute exists in the relationships array, it's instantiated (and, yeah, I plan to move the contained objects into a declared array. one thing at a time :) ) $sales_order = new sales_order; $sales_order->has_a( customer ); print $sales_order->customer->name; The last line fails, because __get is passed only 'customer' and not 'customer->name' - I have no way of knowing what attribute of customer was being accessed, so I can't return the requested data. Running this code: $sales_order = new sales_order; $sales_order->has_a( customer ); print $sales_order->customer->name; print $sales_order->customer->name; the fourth line works, because the customer object was instantiated during line three (line three still fails to return a value). Is this an issue with __get() or is this how it was intended to work? -- Jason N Perkins