note 96566 added to language.oop5.patterns

From: Date: Fri, 05 Mar 2010 15:11:06 +0000
Subject: note 96566 added to language.oop5.patterns
Groups: php.notes 
Request: Send a blank email to php-notes+get-166697@lists.php.net to get a copy of this message
Php needs macros! That's right php seems almost unusable sometimes without precompiler. Fortunately you can generate php code with php and (easily) cache it so you can still take advantage of the php optimizing capabilities. It just creates a mess when debugging, no big deal ;) You need to extend the language to your own needs. But this singleton business might just work without without macros, not that nicely though. <?php class Singleton { private static $instance; public static function singleton($class = __CLASS__) { if (!isset(self::$instance)) self::$instance = array(); if (!isset(self::$instance[$classname])) self::$instance[$classname] = new $classname; return self::$instance[$classname]; } } class SubClass extends Singleton { public static function singleton($class = __CLASS__) { return parent::singleton($class); } } class SubSubClass extends SubClass { public static function singleton($class = __CLASS__) { return parent::singleton($class); } } ?> As you can see, the static $instance can be an associative array holding all the instances of derived classes, but you still can't work around overriding the singleton/getInstance static method... That's what macros or language extensions would be for... <?php class SINGLETON(SubClass) { /*...*/ } ?> Translate to: <?php class SubClass extends Singleton { public static function singleton($class = __CLASS__) { return parent::singleton($class); } /*...*/ } ?> ---- Server IP: 69.147.83.197 Probable Submitter: 89.133.46.146 ---- Manual Page -- http://www.php.net/manual/en/language.oop5.patterns.php Edit -- https://master.php.net/note/edit/96566 Del: integrated -- https://master.php.net/note/delete/96566/integrated Del: useless -- https://master.php.net/note/delete/96566/useless Del: bad code -- https://master.php.net/note/delete/96566/bad+code Del: spam -- https://master.php.net/note/delete/96566/spam Del: non-english -- https://master.php.net/note/delete/96566/non-english Del: in docs -- https://master.php.net/note/delete/96566/in+docs Del: other reasons-- https://master.php.net/note/delete/96566 Reject -- https://master.php.net/note/reject/96566 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#166697) next »