-
Notifications
You must be signed in to change notification settings - Fork 0
/
method_obj.php
57 lines (48 loc) · 1.26 KB
/
method_obj.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
<?php
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
class Hello {
private $name;
private $age;
private $chain;
// Khoi tai gia tri mac dinh bang false
public function __construct($chain = FALSE) {
$this->chain = $chain;
}
public function setName($name) {
if ($this->chain == FALSE) {
return $this->name = $name;
} else {
$this->name = $name;
return $this;
}
}
public function setAge($age) {
if ($this->chain == FALSE) {
return $this->name = $age;
} else {
$this->name = $age;
return $this;
}
}
public function introduce() {
if (empty($this->name)) {
print("I need a name!");
} else if (empty($this->age)) {
print("I dont have an age!");
} else {
printf("Hello there, my name is <b>%s</b> and I am <b>%s</b> years old.", $this->name, $this->age);
}
}
}
//$hello = new Hello();
$name = "Tung";
$age = 17;
//echo $hello->setName($name);
//echo $hello->setAge($age);
//$hello->introduce();
$hello = new Hello(true); // chaining method
$hello->setName($name)->setAge($age)->introduce();
?>