note 95008 added to language.oop5.static

From: Date: Tue, 08 Dec 2009 17:34:55 +0000
Subject: note 95008 added to language.oop5.static
Groups: php.notes 
Request: Send a blank email to php-notes+get-163718@lists.php.net to get a copy of this message
<?php // USING the same method in an instance and in a static context // If you want to have the same definition of a method that you // want to use both as static and as instance method, a classname control of $this // variable is needed. This tip can be used wether the caller context is not the same class type class A { private $myProp = 'A'; public function myMethodA() { if(isset($this) && get_class($this) == __CLASS__) return $this->myProp; return 'C'; } } class B { public $myProp = 'B'; public function myMethodB() { $a = new A; return $a->myMethodA().' ++++ '.A::myMethodA(); } } $b = new B(); echo $b->myMethodB(); // output: A ++++ C // NOTE: // if you don't verify "&& get_class($this) == __CLASS__" // it will output A ++++ B, it seems that if a method is not marked as "static" // its scope would be the scope in which the function was called ?> ---- Server IP: 69.147.83.197 Probable Submitter: 94.163.152.193 ---- Manual Page -- http://www.php.net/manual/en/language.oop5.static.php Edit -- https://master.php.net/note/edit/95008 Del: integrated -- https://master.php.net/note/delete/95008/integrated Del: useless -- https://master.php.net/note/delete/95008/useless Del: bad code -- https://master.php.net/note/delete/95008/bad+code Del: spam -- https://master.php.net/note/delete/95008/spam Del: non-english -- https://master.php.net/note/delete/95008/non-english Del: in docs -- https://master.php.net/note/delete/95008/in+docs Del: other reasons-- https://master.php.net/note/delete/95008 Reject -- https://master.php.net/note/reject/95008 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#163718) next »