aidan           Fri Oct 29 23:39:07 2004 EDT

  Modified files:              
    /phpdoc/en/language/oop5    patterns.xml 
  Log:
  Better examples
  
http://cvs.php.net/diff.php/phpdoc/en/language/oop5/patterns.xml?r1=1.1&r2=1.2&ty=u
Index: phpdoc/en/language/oop5/patterns.xml
diff -u phpdoc/en/language/oop5/patterns.xml:1.1 
phpdoc/en/language/oop5/patterns.xml:1.2
--- phpdoc/en/language/oop5/patterns.xml:1.1    Fri Oct 29 07:28:28 2004
+++ phpdoc/en/language/oop5/patterns.xml        Fri Oct 29 23:39:06 2004
@@ -1,5 +1,5 @@
 <?xml version="1.0" encoding="iso-8859-1"?>
-<!-- $Revision: 1.1 $ -->
+<!-- $Revision: 1.2 $ -->
 <sect1 id="language.oop5.patterns">
  <title>Patterns</title>
  <para>
@@ -19,10 +19,9 @@
    <programlisting role="php">
 <![CDATA[
 <?php
-class DB {
-    // ... Other methods
-
-    // The Factory Method
+class Example
+{
+    // The factory method
     function &factory($type)
     {
         if (include_once 'Drivers/' . $type . '.php') {
@@ -38,17 +37,18 @@
    </programlisting>
    <para>
     Defining this method in a class allows drivers to be loaded on the
-    fly. Loading a <literal>MySQL</literal> and a
+    fly. If the <literal>Example</literal> class was a database
+    abstraction class, loading a <literal>MySQL</literal> and
     <literal>SQLite</literal> driver could be done as follows:
    </para>
    <programlisting role="php">
 <![CDATA[
 <?php
 // Load a MySQL Driver
-$db_mysql = DB::factory('MySQL');
+$mysql = Example::factory('MySQL');
 
 // Load a SQLite Driver
-$db_sqlite = DB::factory('SQLite');
+$sqlite = Example::factory('SQLite');
 ?>
 ]]>
    </programlisting>
@@ -69,30 +69,52 @@
    <programlisting role="php">
 <![CDATA[
 <?php
-function &singleton($class)
-{ 
-    // Declare a static variable to hold the object instance
-    static $instance; 
-
-    // If the instance is not there create one
-    if (!isset($instance)) { 
-        $instance = new $class; 
+class Example
+{
+    // Hold an instance of the class
+    static private $instance;
+    
+    // A private constructor
+    private function __construct() 
+    {
+        echo 'I am constructed';
     }
 
-    return($instance); 
+    // The singleton method
+    static public function singleton() 
+    {
+        if (!isset(self::$instance)) {
+            $c = __CLASS__;
+            self::$instance = new $c;
+        }
+
+        return self::$instance;
+    }
+    
+    // Example method
+    public function bark()
+    {
+        echo 'Woof!';
+    }
 }
 
 ?>
 ]]>
    </programlisting>
    <para>
-    This allows a single instance of any class to be retrieved.
-    Loading an example <literal>DB</literal> could be done like so:
+    This allows a single instance of the <literal>Example</literal>
+    class to be retrieved.
    </para>
    <programlisting role="php">
 <![CDATA[
 <?php
-$db = singleton('DB');
+// This would fail because the constructor is private
+$test = new Example;
+
+// This will always retrieve a single instance of the class
+$test = Example::singleton();
+$test->bark();
+
 ?>
 ]]>
    </programlisting>

Reply via email to