Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:43329 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 8235 invoked from network); 11 Mar 2009 23:09:35 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 11 Mar 2009 23:09:35 -0000 Authentication-Results: pb1.pair.com smtp.mail=greg@chiaraquartet.net; spf=permerror; sender-id=unknown Authentication-Results: pb1.pair.com header.from=greg@chiaraquartet.net; sender-id=unknown Received-SPF: error (pb1.pair.com: domain chiaraquartet.net from 209.85.198.226 cause and error) X-PHP-List-Original-Sender: greg@chiaraquartet.net X-Host-Fingerprint: 209.85.198.226 rv-out-0506.google.com Received: from [209.85.198.226] ([209.85.198.226:60842] helo=rv-out-0506.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 77/84-09289-EA448B94 for ; Wed, 11 Mar 2009 18:09:35 -0500 Received: by rv-out-0506.google.com with SMTP id b25so218713rvf.23 for ; Wed, 11 Mar 2009 16:09:31 -0700 (PDT) Received: by 10.114.24.5 with SMTP id 5mr5526174wax.106.1236812971800; Wed, 11 Mar 2009 16:09:31 -0700 (PDT) Received: from ?192.168.1.28? ([173.9.108.154]) by mx.google.com with ESMTPS id m29sm1572269poh.20.2009.03.11.16.09.27 (version=TLSv1/SSLv3 cipher=RC4-MD5); Wed, 11 Mar 2009 16:09:31 -0700 (PDT) Message-ID: <49B844A5.2000302@chiaraquartet.net> Date: Wed, 11 Mar 2009 18:09:25 -0500 User-Agent: Thunderbird 2.0.0.6 (Macintosh/20070807) MIME-Version: 1.0 To: olivier@ajeux.com CC: Nathan Nobbe , internals@lists.php.net References: <7dd2dc0b0903111512g798af3fco66141a925508b4fd@mail.gmail.com> In-Reply-To: Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit Subject: Re: [PHP-DEV] non static function called as static one From: greg@chiaraquartet.net (Greg Beaver) Olivier Doucet wrote: > Hi, > > >> not sure if this was mentioned on the general list but, i believe what >> youre describing is documented in the manual under php5 classes/objects -> >> "the basics": >> >> http://www.php.net/manual/en/language.oop5.basic.php >> >> $this is a reference to the calling object (usually the object to which >> the method belongs, but can be another object, if the method is called >> statically from >> the context of a secondary object). >> >> -nathan >> > I know this behaviour is fully documented, but I was more concerned about > "is it a 'normal' behaviour ?". How can a programmer controls the class he Yes > wrote, and make it absolutely bugproof ? How can he detect his function was > called in a static context and forbid it (or write specific code for) ? You can't unless you do some obtuse kind of: if (isset($this) && $this instanceof MyClass) But even this results in terrible and subtle mistakes if you happen to call a "static" method from within a method of a subclass of MyClass. instead use static function blah() for static functions, and $this simply will not be available. Don't call non-static methods statically unless you want trouble. "static" was introduced precisely for this reason. Trying to make functions work as both dynamic and static is a disaster, and we have had endless intractable problems with the most prevalent implementation of this double design in the PEAR base class, specifically with PEAR::raiseError(). Just don't do it. Greg