note 85582 added to language.oop5.patterns

From: Date: Sun, 07 Sep 2008 01:49:37 +0000
Subject: note 85582 added to language.oop5.patterns
Groups: php.notes 
Request: Send a blank email to php-notes+get-144206@lists.php.net to get a copy of this message
I've been forward-developing for PHP 5.3, and couldn't help but notice that late-static-binding makes implementing Singletons simple. Its even easy to set up some function forwarding so that static calls are transformed into non-staic calls: <?php // Requires PHP 5.3! class Singleton { public static function Instance() { static $instances = array(); $class = get_called_class(); if (!array_key_exists($class, $instances)) { $instances[$class] = new $class(); } return $instances[$class]; } public static function __callStatic($func, $args) { $class = static::Instance(); return call_user_func_array(array($class, '__'.$func), $args); } } class Earth extends Singleton { public function __DoSomething($a, $b) { return 15 + $this->SomethingElse($a, $b); } protected function SomethingElse($c, $d) { return $c % $d; } } echo Earth::DoSomething(6,27); ?> ---- Server IP: 208.69.120.35 Probable Submitter: 96.231.62.197 ---- Manual Page -- http://www.php.net/manual/en/language.oop5.patterns.php Edit -- https://master.php.net/note/edit/85582 Del: integrated -- https://master.php.net/note/delete/85582/integrated Del: useless -- https://master.php.net/note/delete/85582/useless Del: bad code -- https://master.php.net/note/delete/85582/bad+code Del: spam -- https://master.php.net/note/delete/85582/spam Del: non-english -- https://master.php.net/note/delete/85582/non-english Del: in docs -- https://master.php.net/note/delete/85582/in+docs Del: other reasons-- https://master.php.net/note/delete/85582 Reject -- https://master.php.net/note/reject/85582 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#144206) next »