Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:43328 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 6952 invoked from network); 11 Mar 2009 23:05:43 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 11 Mar 2009 23:05:43 -0000 Authentication-Results: pb1.pair.com smtp.mail=quickshiftin@gmail.com; spf=pass; sender-id=pass Authentication-Results: pb1.pair.com header.from=quickshiftin@gmail.com; sender-id=pass; domainkeys=bad Received-SPF: pass (pb1.pair.com: domain gmail.com designates 209.85.217.165 as permitted sender) DomainKey-Status: bad X-DomainKeys: Ecelerity dk_validate implementing draft-delany-domainkeys-base-01 X-PHP-List-Original-Sender: quickshiftin@gmail.com X-Host-Fingerprint: 209.85.217.165 mail-gx0-f165.google.com Received: from [209.85.217.165] ([209.85.217.165:52290] helo=mail-gx0-f165.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id FF/34-09289-6C348B94 for ; Wed, 11 Mar 2009 18:05:43 -0500 Received: by gxk9 with SMTP id 9so537253gxk.23 for ; Wed, 11 Mar 2009 16:05:40 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:mime-version:received:in-reply-to:references :date:message-id:subject:from:to:cc:content-type; bh=ZwJ1Oe7Cxi1Ia/GC3rrYydmxZbNe8twJ43NUKqH4D8c=; b=cdtCdXFQcVRyU5XoN9p4RaZ6F77H2+CkdnKkAPfp04oDnX2U/UjG8/+TafG4uy13jr mBiP99IF4bC1ogF2q1GDiNxhL3sjdWMZPpL1g+/s+qB5n6jzA68iAU2dK+XCuAoN03cr bsbLkOa+fTeZYyUaQtEogKa/0T3xLlzG/wBF8= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :cc:content-type; b=u6WVfNtQjcI9Qmiq53STsXUYMdsTwSi0N8EGIrHQPS9W/lm8X2yCt8djN6s10lK2y2 j4WIhfpBpn7DNqq+VkFKCEyUPGz9CvZ52Mn2nBGuPpp+JVBnZTAwZsmWUU+/nhCwr44J Qc/IZMemvIKrzs5AmQMpDZQBjtwbmTU0a+8TU= MIME-Version: 1.0 Received: by 10.231.32.70 with SMTP id b6mr2120691ibd.52.1236812740378; Wed, 11 Mar 2009 16:05:40 -0700 (PDT) In-Reply-To: References: <7dd2dc0b0903111512g798af3fco66141a925508b4fd@mail.gmail.com> Date: Wed, 11 Mar 2009 17:05:40 -0600 Message-ID: <7dd2dc0b0903111605v22ab4b29nfdbfbf06bd5b1b27@mail.gmail.com> To: olivier@ajeux.com Cc: internals@lists.php.net Content-Type: multipart/alternative; boundary=002215048d6f0b42510464dfe8ac Subject: Re: [PHP-DEV] non static function called as static one From: quickshiftin@gmail.com (Nathan Nobbe) --002215048d6f0b42510464dfe8ac Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit On Wed, Mar 11, 2009 at 4:40 PM, 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 > 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) ? > well, the access to $this of the secondary object is limited. so the only way it really does anything useful is if member variables you wish to access are marked as public; making it mostly useless imo.. but anyway all youd need to do is mark member vars as protected or private. (personally, i think it should be able to access protected vars, since the class which invokes the other statically is essentially sanctioning access to its instance variables). $varname); } } class B { public $a = 1; protected $b = 2; private $c = 3; public function dumpViaMixin() { A::showOtherClassInstanceVar('a'); A::showOtherClassInstanceVar('b'); A::showOtherClassInstanceVar('c'); } } $b = new B(); $b->dumpViaMixin(); --- outputs int(1) then the error log shows, [11-Mar-2009 17:01:03] PHP Fatal error: Cannot access protected property B::$b in /Users/nnobbe/testDelegation.php on line 5 [11-Mar-2009 17:01:03] PHP Stack trace: [11-Mar-2009 17:01:03] PHP 1. {main}() /Users/nnobbe/testDelegation.php:0 [11-Mar-2009 17:01:03] PHP 2. B->dumpViaMixin() /Users/nnobbe/testDelegation.php:22 [11-Mar-2009 17:01:03] PHP 3. A->showOtherClassInstanceVar() /Users/nnobbe/testDelegation.php:16 so you should be able to protect your classes w/o too much trouble. -nathan --002215048d6f0b42510464dfe8ac--