Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:16245 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 79503 invoked by uid 1010); 14 May 2005 02:05:41 -0000 Delivered-To: ezmlm-scan-internals@lists.php.net Delivered-To: ezmlm-internals@lists.php.net Received: (qmail 67568 invoked by uid 1007); 14 May 2005 01:56:01 -0000 Message-ID: <20050514015601.67567.qmail@lists.php.net> To: internals@lists.php.net Date: Fri, 13 May 2005 21:55:54 -0400 User-Agent: Mozilla Thunderbird 1.0 (Windows/20041206) X-Accept-Language: en-us, en MIME-Version: 1.0 References: In-Reply-To: Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Posted-By: 68.112.247.235 Subject: Re: new overloading feature? From: jhannus@128kb.com (Justin Hannus) My thoughts are: 1. Adding to many __methods is ugly. 2. Interesting subject though. Not to add to the flame but I've seen the need for more advanced overloading features in allot of the code I've been writing with PHP5. Consider the following example (C++ almost like, may slow down the engine but seems like a cleaner idea): class StringObject { public $val = ''; public function __construct($str = '') { $this->val = $str; } public function __operator($operator, &$lOperand, &$rOperand, &$returnValue) { switch ($operator) { case T_CONCAT_EQUALS: if ($rOperand instanceof StringObject && $lOperand instanceof StringObject) { $returnValue = $lOperand->val . $rOperand->val; // or something? return true; // use operator handler; } // ...other operators that are supported by this object // can be defined here... } return false; // normal operation takes place; } } $str1 = new StringObject("hello "); $str2 = new StringObject("world!"); str1 .= $str2; This would be very useful and very clean but seems like if defined would really slow down the engine. But, i think this could work well in many, many different situations. Maybe only a subset of operators are overloadable with this method. -Justin Derrell Lipman wrote: > I came across an interesting desire today. I'd like to create a new class > instance if an only if a "key" value does not already exist. This key value > could be looked up in a database, in an array, etc. > > The following contrived example shows use of a proposed __new() overload > function which would be called BEFORE the constructor, and could chose to > return a newly constructed object (by calling __construct()) or to return an > already existing object. > > One could certainly call a function which searched for the key value and only > instantiated a new object if the existing one was not found, but this seems > cleaner. > > Thoughts? > > > class X > { > static $allX = array(); > var $val; > > function __construct($val) > { > $this->val = $val; > X::$allX[] =& $this; > } > > function __new($val) > { > foreach (X::$allX as $x) > { > if ($x->val == $val) > { > return $x; > } > } > > return __construct($val); > } > } > > $try1 = new X(23); /* would return $allX[0] reference */ > $try2 = new X(42); /* woudl return $allX[1] reference */ > $try3 = new X(23); /* would return $allX[0] reference */ > ?>