Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:50409 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 42822 invoked from network); 22 Nov 2010 11:52:16 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 22 Nov 2010 11:52:16 -0000 Authentication-Results: pb1.pair.com smtp.mail=sebastian@php.net; spf=unknown; sender-id=unknown Authentication-Results: pb1.pair.com header.from=sebastian@php.net; sender-id=unknown Received-SPF: unknown (pb1.pair.com: domain php.net does not designate 93.190.64.36 as permitted sender) X-PHP-List-Original-Sender: sebastian@php.net X-Host-Fingerprint: 93.190.64.36 mail-6.de-punkt.de Received: from [93.190.64.36] ([93.190.64.36:39930] helo=mail-6.de-punkt.de) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 1E/80-13423-D695AEC4 for ; Mon, 22 Nov 2010 06:52:15 -0500 Received: (qmail 15921 invoked by uid 511); 22 Nov 2010 11:52:11 -0000 Received: by simscan 1.3.1 ppid: 15917, pid: 15919, t: 0.0361s scanners: attach: 1.4.0 Received: from unknown (HELO ?192.168.2.31?) (sb@sebastian-bergmann.de@87.185.101.8) by 0 with ESMTPA; 22 Nov 2010 11:52:11 -0000 Message-ID: <4CEA596A.7010700@php.net> Date: Mon, 22 Nov 2010 12:52:10 +0100 User-Agent: Mozilla/5.0 (Windows; U; Windows NT 6.1; de; rv:1.9.2.12) Gecko/20101027 Thunderbird/3.1.6 MIME-Version: 1.0 To: internals@lists.php.net References: In-Reply-To: Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit Subject: Re: [PHP-DEV] Traits and static methods From: sebastian@php.net (Sebastian Bergmann) Am 16.11.2010 18:46, schrieb Simas Toleikis: > p.s. a Singleton-Trait implementation could look like: > > trait Singleton { > public static function getInstance() { ... } > } > > class Child extends Parent { > use Singleton; > } > > Child::getInstance(); This is not really a good example as the getInstance() method alone is not sufficient for a reusable implementation of the Singleton pattern. A trait for a reusable implementation of the Singleton pattern should look more like trait Singleton { private static $uniqueInstance = NULL; protected function __construct() {} private final function __clone() {} public static function getInstance() { if (self::$uniqueInstance === NULL) { self::$uniqueInstance = new Singleton; } return self::$uniqueInstance; } } which of course does not work as traits are stateless and class MySingleton { use Singleton; } MySingleton::getInstance(); results in Access to undeclared static property: MySingleton::$uniqueInstance And rightfully so. Nobody should need a mechanism to make it as easy as pie to clutter the code base with singletons ;-) -- Sebastian Bergmann Co-Founder and Principal Consultant http://sebastian-bergmann.de/ http://thePHP.cc/