Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:3373 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 22608 invoked from network); 12 Jul 2003 00:53:23 -0000 Received: from unknown (HELO alanathome.ossc.com.hk) (202.81.246.113) by pb1.pair.com with SMTP; 12 Jul 2003 00:53:23 -0000 Received: from devel ([192.168.0.40] helo=akbkhome.com ident=alan) by alanathome.ossc.com.hk with esmtp (Exim 4.20) id 19b8f8-0006X0-Cb; Sat, 12 Jul 2003 08:54:54 +0800 Message-ID: <3F0F5C41.4080002@akbkhome.com> Date: Sat, 12 Jul 2003 08:54:25 +0800 User-Agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.4b) Gecko/20030520 X-Accept-Language: en-us, en MIME-Version: 1.0 To: Josh Fuhs CC: internals@lists.php.net References: <1057959512.3f0f2e5829ec6@webmail.purdue.edu> In-Reply-To: <1057959512.3f0f2e5829ec6@webmail.purdue.edu> Content-Type: text/plain; charset=us-ascii; format=flowed Content-Transfer-Encoding: 7bit Subject: Re: [PHP-DEV] Weird behavior for $this variable in PHP4 From: alan@akbkhome.com (Alan Knowles) in php4 $this gets passed in All static method calls ,as if they where parent::sss() - so statically calling one object method from another object instance will take '$this' with it.. Last word on this 'feature/bug' is that it will be removed in PHP5, (bit of a bugger - I'm sure some of my old code relied on it ;) Regards Alan Josh Fuhs wrote: > Hello, > > I'm getting a problem with $this being defined where it shouldn't be. The code > that I'm writing is a little weird in order to compensate for the lack of some > OO features in PHP4. > > The PHP script at the bottom should give the behavior I'm referring to. > > Is PHP5 in a more-or-less stable state? Are PHP4 extensions completely > compatible with PHP5? > > Josh > > > /* No static attributes in PHP4. */ > $static_class1=NULL; > > class Class1{ > var $var1; > function Class1(){ $this->var1=0; } > > function init(){ > global $static_class1; > $static_class1 = new Class1(); > } > > function getVar(){ > /* Check for $this. This is a way of making a static method > and a regular method with the same name. */ > /* $this is defined here directly after called from the > Class2 constructor. */ > if(isset($this)){ > return $this->var1; > } else { > global $static_class1; > $static_class1->getVar(); > } > } > } > > class Class2{ > function Class2(){ > $a=Class1::getVar(); > } > } > > Class1::init(); > $a=new Class2(); > > ?> > > -- Can you help out? Need Consulting Services or Know of a Job? http://www.akbkhome.com