01 |
|
02 |
<?php |
03 |
class Fruit { |
04 |
// 對象從工廠類返回 |
05 |
} |
06 |
|
07 |
Class FruitFactory { |
08 |
|
09 |
public static function factory() { |
10 |
// 返回對象的一個新實例 |
11 |
return new Fruit(); |
12 |
} |
13 |
} |
14 |
|
15 |
// 調用工廠 |
16 |
$instance = FruitFactory::factory(); |
17 |
?> |
01 |
|
02 |
<?php |
03 |
class Example |
04 |
{ |
05 |
// The parameterized factory method |
06 |
public static function factory( $type ) |
07 |
{ |
08 |
if ( include_once 'Drivers/' . $type . '.php' ) { |
09 |
$classname = 'Driver_' . $type ; |
10 |
return new $classname ; |
11 |
} else { |
12 |
throw new Exception( 'Driver not found' ); |
13 |
} |
14 |
} |
15 |
} |
16 |
|
17 |
// Load a MySQL Driver |
18 |
$mysql = Example::factory( 'MySQL' ); |
19 |
|
20 |
// Load an SQLite Driver |
21 |
$sqlite = Example::factory( 'SQLite' ); |
22 |
?> |
01 |
<?php |
02 |
|
03 |
/** |
04 |
* Generic Factory class |
05 |
* |
06 |
* This Magic Factory will remember all operations you perform on it, |
07 |
* and apply them to the object it instantiates. |
08 |
* |
09 |
*/ |
10 |
class FruitFactory { |
11 |
private $history , $class , $constructor_args ; |
12 |
|
13 |
/** |
14 |
* Create a factory of given class. Accepts extra arguments to be passed to |
15 |
* class constructor. |
16 |
*/ |
17 |
function __construct( $class ) { |
18 |
$args = func_get_args(); |
19 |
$this -> class = $class ; |
20 |
$this ->constructor_args = array_slice ( $args , 1 ); |
21 |
} |
22 |
|
23 |
function __call( $method , $args ) { |
24 |
$this ->history[] = array ( |
25 |
'action' => 'call' , |
26 |
'method' => $method , |
27 |
'args' => $args |
28 |
); |
29 |
} |
30 |
|
31 |
function __set( $property , $value ) { |
32 |
$this ->history[] = array ( |
33 |
'action' => 'set' , |
34 |
'property' => $property , |
35 |
'value' => $value |
36 |
); |
37 |
} |
38 |
|
39 |
/** |
40 |
* Creates an instance and performs all operations that were done on this MagicFactory |
41 |
*/ |
42 |
function instance() { |
43 |
# use Reflection to create a new instance, using the $args |
44 |
$reflection_object = new ReflectionClass( $this -> class ); |
45 |
$object = $reflection_object ->newInstanceArgs( $this ->constructor_args ); |
46 |
|
47 |
# Alternative method that doesn 't use ReflectionClass, but doesn' t support variable |
48 |
# number of constructor parameters. |
49 |
//$object = new $this->class(); |
50 |
|
51 |
# Repeat all remembered operations, apply to new object. |
52 |
foreach ( $this ->history as $item ) { |
53 |
if ( $item [ 'action' ] == 'call' ) { |
54 |
call_user_func_array( array ( $object , $item [ 'method' ] ), $item [ 'args' ] ); |
55 |
} |
56 |
if ( $item [ 'action' ] == 'set' ) { |
57 |
$object ->{ $item [ 'property' ]} = $item [ 'value' ]; |
58 |
} |
59 |
} |
60 |
|
61 |
# Done |
62 |
return $object ; |
63 |
} |
64 |
} |
65 |
|
66 |
class Fruit { |
67 |
private $name , $color ; |
68 |
public $price ; |
69 |
|
70 |
function __construct( $name , $color ) { |
71 |
$this ->name = $name ; |
72 |
$this ->color = $color ; |
73 |
} |
74 |
|
75 |
function setName( $name ) { |
76 |
$this ->name = $name ; |
77 |
} |
78 |
|
79 |
function introduce() { |
80 |
print "Hello, this is an {$this->name} {$this->sirname}, its price is {$this->price} RMB." ; |
81 |
} |
82 |
} |
83 |
|
84 |
# Setup a factory |
85 |
$fruit_factory = new FruitFactory( 'Fruit' , 'Apple' , 'Gonn' ); |
86 |
$fruit_factory ->setName( 'Apple' ); |
87 |
$fruit_factory ->price = 2; |
88 |
|
89 |
# Get an instance |
90 |
$apple = $fruit_factory ->instance(); |
91 |
$apple ->introduce(); |
92 |
|
93 |
?> |
1 |
Hello, this is an Apple , its price is 2 RMB. |