Skip to content

Commit 2fa0341

Browse files
committed
Example prototype code complete.
1 parent 2f91878 commit 2fa0341

File tree

4 files changed

+106
-0
lines changed

4 files changed

+106
-0
lines changed
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<?php
2+
//Client.php
3+
function __autoload($class_name) {
4+
include $class_name . '.php';
5+
}
6+
7+
class Client {
8+
//For direct instantiation
9+
private $fly1;
10+
11+
private $fly2;
12+
13+
//For cloning
14+
private $c1Fly;
15+
16+
private $c2Fly;
17+
18+
private $updatedCloneFly;
19+
20+
public function __construct() {
21+
//Instantiate
22+
$this->fly1=new MaleProto();
23+
$this->fly2=new FemaleProto();
24+
//Clone
25+
$this->c1Fly = clone $this->fly1;
26+
$this->c2Fly = clone $this->fly2;
27+
$this->updatedCloneFly = clone $this->fly2;
28+
//update clones
29+
$this->c1Fly->mated="true";
30+
$this->c2Fly->fecundity="186";
31+
$this->updatedCloneFly->eyeColor="purple";
32+
$this->updatedCloneFly->wingBeat="220";
33+
$this->updatedCloneFly->unitEyes="750";
34+
$this->updatedCloneFly->fecundity="92";
35+
//Send through type hinting method
36+
$this->showFly($this->c1Fly);
37+
$this->showFly($this->c2Fly);
38+
$this->showFly($this->updatedCloneFly);
39+
}
40+
41+
private function showFly(IPrototype $fly) {
42+
echo "Eye color: " . $fly->eyeColor . "<br/>";
43+
echo "Wing Beats/second: " . $fly->wingBeat . "<br/>";
44+
echo "Eye units: " . $fly->unitEyes . "<br/>";
45+
$genderNow=$fly::gender;
46+
echo "Gender: " . $genderNow . "<br/>";
47+
if($genderNow=="FEMALE") {
48+
echo "Number of eggs: " . $fly->fecundity . "<p/>";
49+
} else {
50+
echo "Mated: " . $fly->mated . "<p/>";
51+
}
52+
}
53+
}
54+
55+
$worker=new Client();
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
//MaleProto.php
4+
include_once('IPrototype.php');
5+
6+
class MaleProto extends IPrototype {
7+
8+
const gender="MALE";
9+
10+
public $mated;
11+
12+
public function __construct() {
13+
$this->eyeColor="red";
14+
$this->wingBeat="220";
15+
$this->unitEyes="760 ";
16+
}
17+
18+
function __clone(){}
19+
20+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
//IPrototype.php
4+
abstract class IPrototype {
5+
6+
public $eyeColor;
7+
8+
public $wingBeat;
9+
10+
public $unitEyes;
11+
12+
abstract function __clone();
13+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
//FemaleProto.php
3+
include_once('IPrototype.php');
4+
5+
class FemaleProto extends IPrototype {
6+
7+
const gender="FEMALE";
8+
9+
public $fecundity;
10+
11+
public function __construct() {
12+
$this->eyeColor="red";
13+
$this->wingBeat="220";
14+
$this->unitEyes="760 ";
15+
}
16+
17+
function __clone(){}
18+
}

0 commit comments

Comments
 (0)