<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
		>
<channel>
	<title>Kommentare zu: Abstract Singleton</title>
	<atom:link href="http://www.phphatesme.com/blog/softwaretechnik/abstract-singleton/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.phphatesme.com/blog/softwaretechnik/abstract-singleton/</link>
	<description>PhpHatesMe, but that&#039;s ok!</description>
	<lastBuildDate>Mon, 06 Feb 2012 20:59:49 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
	<item>
		<title>Von: Manuel Grundner</title>
		<link>http://www.phphatesme.com/blog/softwaretechnik/abstract-singleton/comment-page-1/#comment-31458</link>
		<dc:creator>Manuel Grundner</dc:creator>
		<pubDate>Sat, 16 May 2009 16:38:51 +0000</pubDate>
		<guid isPermaLink="false">http://www.phphatesme.com/?p=539#comment-31458</guid>
		<description>Ich seh grad das is was aus nem alten commit hochgekommen..

Die iterator methode kann man streichen, dann spart man sich die abfrage im getMethods, weil im getIterator eh kein $this aufgerufen wird (da wären wir wieder bei einem älteren eintrag von euch ^^, in dem fall sogar sinnvoll einsetzbar)

es ginge dann sogar:
foreach (OutputTypes::xhtml() as $outputType)
echo $outputType;

aber das ist schwer zu lesen, und irreführend.</description>
		<content:encoded><![CDATA[<p>Ich seh grad das is was aus nem alten commit hochgekommen..</p>
<p>Die iterator methode kann man streichen, dann spart man sich die abfrage im getMethods, weil im getIterator eh kein $this aufgerufen wird (da wären wir wieder bei einem älteren eintrag von euch ^^, in dem fall sogar sinnvoll einsetzbar)</p>
<p>es ginge dann sogar:<br />
foreach (OutputTypes::xhtml() as $outputType)<br />
echo $outputType;</p>
<p>aber das ist schwer zu lesen, und irreführend.</p>
]]></content:encoded>
	</item>
	<item>
		<title>Von: Manuel Grundner</title>
		<link>http://www.phphatesme.com/blog/softwaretechnik/abstract-singleton/comment-page-1/#comment-31457</link>
		<dc:creator>Manuel Grundner</dc:creator>
		<pubDate>Sat, 16 May 2009 16:33:06 +0000</pubDate>
		<guid isPermaLink="false">http://www.phphatesme.com/?p=539#comment-31457</guid>
		<description>ja, da hast du vollkommen recht, nur mit singletons muss/soll man ohnehin sparsam umgehen, nur beim enum gehts nicht ohne.

Via Reflection ist sogar ein iterierbarer Enum machbar (Delphi like)
&lt;pre&gt;&lt;code&gt;namespace HazAClass\type;

use HazAClass\type\exception\IteratorMethodWasTriedToUsedAsEnumValue;

/**
 * Base-Class for Typesave Enum
 */
abstract class Enum implements \IteratorAggregate, \Countable
{
	/**
	 * Der Name der Klasse
	 *
	 * @var string classname
	 */
	public static $classname = __CLASS__;

	/**
	 * Value of enum
	 *
	 * @var string
	 */
	private $value;

	/**
	 * The Methods/Values in the Enum
	 *
	 * @var array[Enum]
	 */
	private static $methods;

	/**
	 * Sets value of enum
	 *
	 * @param string $value
	 */
	final protected function __construct($value)
	{
		$this-&gt;value = $value;
	}

	/**
	 * Returns an &#039;null&#039;-enum used to iterate a enum.
	 *
	 * Note: if you try to use this enum-value as an &#039;real&#039; enum
	 * an exception is thrown in the getValue or __toString method!
	 *
	 * @return Enum
	 */
	final public static function iterator()
	{
		return new static::$classname(null);
	}


	/**
	 * Returns the current value of the enum
	 *
	 * @see iterator
	 * @throws IteratorMethodWasTriedToUsedAsEnumValue
	 *
	 * @return string
	 */
	final public function getValue()
	{
		if(is_null($this-&gt;value))
			throw new IteratorMethodWasTriedToUsedAsEnumValue(&#039;The iterator-method can only be used to iterate, not as an value&#039;);
		return $this-&gt;value;
	}

	/**
	 * Casts Enum to String
	 *
	 * @return string
	 */
	final public function __toString()
	{
		if(is_null($this-&gt;getValue()))
			return &#039;&#039;;
		return $this-&gt;getValue();
	}

	/**
	 * @see Countable::count()
	 *
	 */
	public function count()
	{
		return count(self::getMethods());
	}

	/**
	 * Returns the values (methods) in the enum
	 *
	 * @return array[methods]
	 */
	private static function getMethods()
	{
		if(is_null(self::$methods))
		{
			$ref = new \ReflectionClass(static::$classname);

			foreach($ref-&gt;getMethods(\ReflectionMethod::IS_PUBLIC) as $method)
			{
				if($method-&gt;isStatic())
				{
					$name = $method-&gt;getName();
					if($name != &#039;iterator&#039;)
					{
						$classname = static::$classname;
						self::$methods[$name] = $classname::$name();
					}
				}
			}
		}
		return self::$methods;
	}

	/**
	 * @see IteratorAggregate::getIterator()
	 */
	public function getIterator()
	{
		return new \ArrayIterator(self::getMethods());
	}

}&lt;/code&gt;&lt;/pre&gt;

das is halt schon a recht komplexe implementierung, aber dann kann man folgendes machen:
&lt;pre&gt;&lt;code&gt;
namespace HazAClass\output;

use HazAClass\type\Enum;

/**
 * Classdescription
 */
class OutputTypes extends Enum
{
	/**
	 * The Name of the class
	 *
	 * @var string $classname
	 */
	public static $classname = __CLASS__;

	/**
	 * xhtml
	 *
	 * @return OutputTypes
	 */
	public static function xhtml()
	{
		return new self::$classname(&#039;xhtml&#039;);
	}

	/**
	 * xml
	 *
	 * @return OutputTypes
	 */
	public static function xml()
	{
		return new self::$classname(&#039;xml&#039;);
	}
}&lt;/code&gt;&lt;/pre&gt;


und dann:
&lt;pre&gt;&lt;code&gt;foreach (OutputTypes::getIterator() as $outputType)
	echo $outputType;&lt;/code&gt;&lt;/pre&gt;

Das schöne ist, das die wirklich typensicher sind

also 

&lt;pre&gt;&lt;code&gt;public function render(OutputTypes $type)
{
//Do Something here
}&lt;/code&gt;&lt;/pre&gt;

Hoffe das ist klar verständlich soweit, sonst einfach rühren ^^</description>
		<content:encoded><![CDATA[<p>ja, da hast du vollkommen recht, nur mit singletons muss/soll man ohnehin sparsam umgehen, nur beim enum gehts nicht ohne.</p>
<p>Via Reflection ist sogar ein iterierbarer Enum machbar (Delphi like)</p>
<pre><code>namespace HazAClass\type;

use HazAClass\type\exception\IteratorMethodWasTriedToUsedAsEnumValue;

/**
 * Base-Class for Typesave Enum
 */
abstract class Enum implements \IteratorAggregate, \Countable
{
	/**
	 * Der Name der Klasse
	 *
	 * @var string classname
	 */
	public static $classname = __CLASS__;

	/**
	 * Value of enum
	 *
	 * @var string
	 */
	private $value;

	/**
	 * The Methods/Values in the Enum
	 *
	 * @var array[Enum]
	 */
	private static $methods;

	/**
	 * Sets value of enum
	 *
	 * @param string $value
	 */
	final protected function __construct($value)
	{
		$this-&gt;value = $value;
	}

	/**
	 * Returns an 'null'-enum used to iterate a enum.
	 *
	 * Note: if you try to use this enum-value as an 'real' enum
	 * an exception is thrown in the getValue or __toString method!
	 *
	 * @return Enum
	 */
	final public static function iterator()
	{
		return new static::$classname(null);
	}

	/**
	 * Returns the current value of the enum
	 *
	 * @see iterator
	 * @throws IteratorMethodWasTriedToUsedAsEnumValue
	 *
	 * @return string
	 */
	final public function getValue()
	{
		if(is_null($this-&gt;value))
			throw new IteratorMethodWasTriedToUsedAsEnumValue('The iterator-method can only be used to iterate, not as an value');
		return $this-&gt;value;
	}

	/**
	 * Casts Enum to String
	 *
	 * @return string
	 */
	final public function __toString()
	{
		if(is_null($this-&gt;getValue()))
			return '';
		return $this-&gt;getValue();
	}

	/**
	 * @see Countable::count()
	 *
	 */
	public function count()
	{
		return count(self::getMethods());
	}

	/**
	 * Returns the values (methods) in the enum
	 *
	 * @return array[methods]
	 */
	private static function getMethods()
	{
		if(is_null(self::$methods))
		{
			$ref = new \ReflectionClass(static::$classname);

			foreach($ref-&gt;getMethods(\ReflectionMethod::IS_PUBLIC) as $method)
			{
				if($method-&gt;isStatic())
				{
					$name = $method-&gt;getName();
					if($name != 'iterator')
					{
						$classname = static::$classname;
						self::$methods[$name] = $classname::$name();
					}
				}
			}
		}
		return self::$methods;
	}

	/**
	 * @see IteratorAggregate::getIterator()
	 */
	public function getIterator()
	{
		return new \ArrayIterator(self::getMethods());
	}

}</code></pre>
<p>das is halt schon a recht komplexe implementierung, aber dann kann man folgendes machen:</p>
<pre><code>
namespace HazAClass\output;

use HazAClass\type\Enum;

/**
 * Classdescription
 */
class OutputTypes extends Enum
{
	/**
	 * The Name of the class
	 *
	 * @var string $classname
	 */
	public static $classname = __CLASS__;

	/**
	 * xhtml
	 *
	 * @return OutputTypes
	 */
	public static function xhtml()
	{
		return new self::$classname('xhtml');
	}

	/**
	 * xml
	 *
	 * @return OutputTypes
	 */
	public static function xml()
	{
		return new self::$classname('xml');
	}
}</code></pre>
<p>und dann:</p>
<pre><code>foreach (OutputTypes::getIterator() as $outputType)
	echo $outputType;</code></pre>
<p>Das schöne ist, das die wirklich typensicher sind</p>
<p>also </p>
<pre><code>public function render(OutputTypes $type)
{
//Do Something here
}</code></pre>
<p>Hoffe das ist klar verständlich soweit, sonst einfach rühren ^^</p>
]]></content:encoded>
	</item>
	<item>
		<title>Von: Nils Langner</title>
		<link>http://www.phphatesme.com/blog/softwaretechnik/abstract-singleton/comment-page-1/#comment-31452</link>
		<dc:creator>Nils Langner</dc:creator>
		<pubDate>Sat, 16 May 2009 14:52:43 +0000</pubDate>
		<guid isPermaLink="false">http://www.phphatesme.com/?p=539#comment-31452</guid>
		<description>@manuel: Erstmal Danke für das Kompliment. Kannst du mal ein Beispiel zu dem ENUM Vorteil bringen? Das ist mir irgendwie noch nicht so ganz klar. Denn ohne das Bsp. finde ich unsere Implementierung schöner, da wir keine protected Variable mitschleppen müssen.</description>
		<content:encoded><![CDATA[<p>@manuel: Erstmal Danke für das Kompliment. Kannst du mal ein Beispiel zu dem ENUM Vorteil bringen? Das ist mir irgendwie noch nicht so ganz klar. Denn ohne das Bsp. finde ich unsere Implementierung schöner, da wir keine protected Variable mitschleppen müssen.</p>
]]></content:encoded>
	</item>
	<item>
		<title>Von: Manuel Grundner</title>
		<link>http://www.phphatesme.com/blog/softwaretechnik/abstract-singleton/comment-page-1/#comment-31442</link>
		<dc:creator>Manuel Grundner</dc:creator>
		<pubDate>Sat, 16 May 2009 11:39:03 +0000</pubDate>
		<guid isPermaLink="false">http://www.phphatesme.com/?p=539#comment-31442</guid>
		<description>Hallo Leute!
Ich beobachte diesen Blog schon längere Zeit und ich muss sagen hier sitzen wirklich fähige Autoren dahinter!

Nun zu meinem Kommentar:
Ich entwickle schon seit anbeginn von PHP5.3 ein recht großes Framework für PHP5.3

Meine Singleton Implementation sieht ein wenig anders aus:

&lt;pre&gt;&lt;code&gt;/**
 * Abstract Singleton Class
 */
abstract class Singleton
{
	/**
	 * The name of the class
	 *
	 * @var string classname
	 */
	public static $classname = __CLASS__;
	
	/**
	 * The instance of the class
	 *
	 * @var Singleton
	 */
	protected static $instance;
	
	/**
	 * Returns the instance
	 *
	 * @return Singleton
	 */
	public static function getInstance()
	{
		if(!is_null(static::$instance))
			static::$instance = new static::$classname();
			
		return static::$instance;
	}
	
	/**
	 * Constructor. Singleton.
	 */
	protected function __construct()
	{
		
	}
	
	
	/**
	 * Cloning is not allowed
	 */
	final private function __clone()
	{
		
	}
}

/**
 * A Counter
 */
class Counter extends Singleton  
{
	/**
	 * The name of the class
	 *
	 * @var string
	 */
	public static $classname = __CLASS__;
	
	/**
	 * The instance of the class
	 *
	 * @var Singleton
	 */
	protected static $instance;
	
	/**
	 * Constructor. Singleton.
	 */
	protected function __construct()
	{
		//Do some stuff here
	}
	
	/**
	 * Returns the count
	 *
	 * @return int
	 */
	public function count()
	{
		//Or Here
	}
}&lt;/code&gt;&lt;/pre&gt;

Nutzt halt LSB.
Das schöne is, damit kann man &quot;schöne&quot; Enums in PHP realisieren.
Nachteil ist natürlich das man den Klassennamen und die Instanzvariable in jeder abgeleiteten Klasse hinterlegen muss.

Bei mir ist das mit dem Klassennamen nicht so das Problem, weil diese Variable bei mir ohnehin in jeder Klasse vorhanden ist.

Grüße aus Österreich
Manuel</description>
		<content:encoded><![CDATA[<p>Hallo Leute!<br />
Ich beobachte diesen Blog schon längere Zeit und ich muss sagen hier sitzen wirklich fähige Autoren dahinter!</p>
<p>Nun zu meinem Kommentar:<br />
Ich entwickle schon seit anbeginn von PHP5.3 ein recht großes Framework für PHP5.3</p>
<p>Meine Singleton Implementation sieht ein wenig anders aus:</p>
<pre><code>/**
 * Abstract Singleton Class
 */
abstract class Singleton
{
	/**
	 * The name of the class
	 *
	 * @var string classname
	 */
	public static $classname = __CLASS__;

	/**
	 * The instance of the class
	 *
	 * @var Singleton
	 */
	protected static $instance;

	/**
	 * Returns the instance
	 *
	 * @return Singleton
	 */
	public static function getInstance()
	{
		if(!is_null(static::$instance))
			static::$instance = new static::$classname();

		return static::$instance;
	}

	/**
	 * Constructor. Singleton.
	 */
	protected function __construct()
	{

	}

	/**
	 * Cloning is not allowed
	 */
	final private function __clone()
	{

	}
}

/**
 * A Counter
 */
class Counter extends Singleton
{
	/**
	 * The name of the class
	 *
	 * @var string
	 */
	public static $classname = __CLASS__;

	/**
	 * The instance of the class
	 *
	 * @var Singleton
	 */
	protected static $instance;

	/**
	 * Constructor. Singleton.
	 */
	protected function __construct()
	{
		//Do some stuff here
	}

	/**
	 * Returns the count
	 *
	 * @return int
	 */
	public function count()
	{
		//Or Here
	}
}</code></pre>
<p>Nutzt halt LSB.<br />
Das schöne is, damit kann man &#8220;schöne&#8221; Enums in PHP realisieren.<br />
Nachteil ist natürlich das man den Klassennamen und die Instanzvariable in jeder abgeleiteten Klasse hinterlegen muss.</p>
<p>Bei mir ist das mit dem Klassennamen nicht so das Problem, weil diese Variable bei mir ohnehin in jeder Klasse vorhanden ist.</p>
<p>Grüße aus Österreich<br />
Manuel</p>
]]></content:encoded>
	</item>
	<item>
		<title>Von: Nils Langner</title>
		<link>http://www.phphatesme.com/blog/softwaretechnik/abstract-singleton/comment-page-1/#comment-56</link>
		<dc:creator>Nils Langner</dc:creator>
		<pubDate>Tue, 16 Sep 2008 04:51:32 +0000</pubDate>
		<guid isPermaLink="false">http://www.phphatesme.com/?p=539#comment-56</guid>
		<description>Morgen Frank, da muss ich dir absolut recht geben, bei allen deinen Punkten. Die beiden Fehler im Code habe ich behoben, dass ich nicht immer ein Freund des Singleton Musters bin, habe ich ja auch schon geschrieben. 
Die Traits Idee werde ich auch jeden Fall noch mal aufgreifen, finde ich ziemlich &quot;sexy&quot;. </description>
		<content:encoded><![CDATA[<p>Morgen Frank, da muss ich dir absolut recht geben, bei allen deinen Punkten. Die beiden Fehler im Code habe ich behoben, dass ich nicht immer ein Freund des Singleton Musters bin, habe ich ja auch schon geschrieben.<br />
Die Traits Idee werde ich auch jeden Fall noch mal aufgreifen, finde ich ziemlich &#8220;sexy&#8221;.</p>
]]></content:encoded>
	</item>
	<item>
		<title>Von: Frank Kleine</title>
		<link>http://www.phphatesme.com/blog/softwaretechnik/abstract-singleton/comment-page-1/#comment-55</link>
		<dc:creator>Frank Kleine</dc:creator>
		<pubDate>Mon, 15 Sep 2008 18:26:07 +0000</pubDate>
		<guid isPermaLink="false">http://www.phphatesme.com/?p=539#comment-55</guid>
		<description>Apropos nicht genug Zeit: das müsste __clone() sein, nicht clone(). Und die Eigenschaft $instance sollte als static deklariert sein. :-)

Ich kann zwar den Hintergrund verstehen, nämlich dass man nicht immer wieder ein Singleton neu implementieren möchte, finden den Ansatz aber trotzdem nicht gut. Zum einen denke ich ist es wenig sinnvoll, etwas vom Typ Singleton zu haben (wobei sollte das helfen?), zum anderen bekommt man damit eine Hierarchie in alle Singletons, die eine Abhängigkeit darstellt welche so nicht gegeben ist.

IMHO wäre es sinnvoller zu warten bis Traits verfügbar sind, und das dann darüber zu lösen.</description>
		<content:encoded><![CDATA[<p>Apropos nicht genug Zeit: das müsste __clone() sein, nicht clone(). Und die Eigenschaft $instance sollte als static deklariert sein. <img src='http://www.phphatesme.com/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /> </p>
<p>Ich kann zwar den Hintergrund verstehen, nämlich dass man nicht immer wieder ein Singleton neu implementieren möchte, finden den Ansatz aber trotzdem nicht gut. Zum einen denke ich ist es wenig sinnvoll, etwas vom Typ Singleton zu haben (wobei sollte das helfen?), zum anderen bekommt man damit eine Hierarchie in alle Singletons, die eine Abhängigkeit darstellt welche so nicht gegeben ist.</p>
<p>IMHO wäre es sinnvoller zu warten bis Traits verfügbar sind, und das dann darüber zu lösen.</p>
]]></content:encoded>
	</item>
</channel>
</rss>

