interface people {
public function say ( ) ;
}
class man implements people {
public function say ( ) {
echo "i am man \n" ;
}
}
class woman implements people {
public function say ( ) {
echo "i am woman \n" ;
}
}
class factory {
public function createMan ( ) {
return new man ( ) ;
}
public function createWoman ( ) {
return new woman ( ) ;
}
}
$f = new factory ( ) ;
$f - > createMan ( ) - > say ( ) ;
$f - > createWoman ( ) - > say ( ) ;
interface db {
public function connect ( ) ;
public function query ( ) ;
public function close ( ) ;
}
class mysqlA implements db {
public $db ;
public function connect ( ) {
$this - > db = mysqli_connect ( $host , $user , $password , $database , $port , $socket ) ;
}
public function query ( ) {
}
public function close ( ) {
mysqli_close ( $this - > db ) ;
}
}
class mysqlB implements db {
public $db ;
public function connect ( ) {
$this - > db = new PDO ( $dsn , $username , $password ) ;
}
public function query ( ) {
}
public function close ( ) {
$this - > db = null ;
}
}
7 class CD
8 {
9 public $trackList ;
10 function __construct ( )
11 {
12
13 $this - > trackList = array ( ) ;
14 }
15
16 public function addTrack ( $track ) {
17 $this - > trackList [ ] = $track ;
18 }
19
20 public function getTrackList ( ) {
21 $output = " " ;
22 foreach ( $this - > trackList as $key = > $value ) {
23
24 $output . = ( $key + 1 ) . ") { $value } . " ;
25 }
26 return $output ;
27 }
28 }
29
30
35 class CDTrackListDecoratorCaps {
36 private $_cd ;
37
38 public function __construct ( CD $CD ) {
39 $this - > _cd = $CD ;
40 }
41
42 public function makeCaps ( ) {
43 foreach ( $this - > _cd - > trackList as $key = > $value ) {
44
45 $this - > _cd - > trackList [ $key ] = strtoupper ( $value ) ;
46 }
47 }
48 }
49
50
51
52 $myCD = new CD ( ) ;
53
54 $trackList = array (
55 "what It Means" ,
56 "brr" ,
57 "goodBye"
58 ) ;
59 foreach ( $trackList as $key = > $value ) {
60
61 $myCD - > addTrack ( $value ) ;
62 }
63 $myCDCaps = new CDTrackListDecoratorCaps ( $myCD ) ;
64 $myCDCaps - > makeCaps ( ) ;
65 print "The CD contains the following tracks:" . $myCD - > getTrackList ( ) ;
11 class man
12 {
13
14 private static $_instance ;
15
16
17 private function __construct ( )
18 {
19 echo '我被实例化了!' ;
20 }
21
22
23 public static function get_instance ( )
24 {
25 var_dump ( isset ( self: : $_instance ) ) ;
26
27 if ( ! isset ( self: : $_instance ) )
28 {
29 self: : $_instance = new self ( ) ;
30 }
31 return self: : $_instance ;
32 }
33
34
35 private function __clone ( )
36 {
37 trigger_error ( 'Clone is not allow' , E_USER_ERROR ) ;
38 }
39
40 function test ( )
41 {
42 echo ( "test" ) ;
43
44 }
45 }
46
47
48
49
50
51 $test = man: : get_instance ( ) ;
52 $test = man: : get_instance ( ) ;
53 $test - > test ( ) ;
54
55
56