mirror of
https://github.com/sigmasternchen/php-doc-en
synced 2025-03-16 00:48:54 +00:00
Better examples
git-svn-id: https://svn.php.net/repository/phpdoc/en/trunk@171604 c90b9560-bf6c-de11-be94-00142212c4b1
This commit is contained in:
parent
3faa2d36b5
commit
a9bf0ded6c
1 changed files with 42 additions and 20 deletions
|
@ -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 @@ class DB {
|
|||
</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 @@ $db_sqlite = DB::factory('SQLite');
|
|||
<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>
|
||||
|
|
Loading…
Reference in a new issue