You are on page 1of 6

OOP in PHP 2

2
)+ )+

(Class)

+(Encapsulation)

(Sub-Class)

(Inheritance)

+ (Object)

(Polymorphism)

(Messaging)

+) (Properties)

(Message)

) (Method Behavior)

P
R P
)

(Constructor Method Behavior)


) )
+ +) +
) PHP P ))
+
1.
2.
3.
4.
5.
6.
7.
8.

<?php
class class_name {
function class_name([parameter]){
....
[return ....;]
}
}
?>

+ 1
+ +R
) , + 170 .., 60 .. + 21 ^
+ +
1. <?php
2. class human {
3.
var $name = human;// default name = human
4.
var $Sex= 1; // default = 1 and 1 = male, 2 = female
5.
var $height = 0; // height > 0
6.
var $weight = 0; // weight > 0
7.
var $age
= 0; // age > 0
8.
function human($name, $sex){

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.?>

$this->setName($name);
$this->setSex($sex);
$this->setHeight(170);
$this->setWeight(60);
$this->setAge(21);
}
function setName($value = 'human'){
$this->name = $value;
}
function setSex($value = 1){
$this->Sex = ($value==1 || $value==2 ? $value : 1);
}
function setHeight($value = 1){
$this->height = ($value > 0 ? $value : 1 );
}
function setWeight($value = 1){
$this->weight = ($value > 0 ? $value : 1 );
}
function setAge($value = 1){
$this->age = ($value > 0 ? $value : 1 );
}
function getName(){
return $this->name;
}
function getSex(){
return ($this->Sex == 1 ? 'male' : 'female');
}
function getHeight(){
return $this->height;
}
function getWeight(){
return($this->Sex == 2 ? 'No Answer':$this->weight);
}
function getAge(){
return($this->Sex == 2 ? 'No Answer':$this->age);
}

+ 2
+ 1 8 14 )) +
P P
1.
2.
3.
4.
5.
6.

$somsak = new human('Somsak', 1);


echo 'Name : '.$somsak->getName().'<br />';
echo 'Sex : '.$somsak->getSex().'<br />';
echo 'Height : '.$somsak->getHeight().'<br />';
echo 'Weight : '.$somsak->getWeight().'<br />';
echo 'Age : '.$somsak->getAge().'<br />';

+ 3

+ 3 1
Name : Somsak
Sex : male
Height : 170
Weight : 60
Age : 21

1
+ 2 3 1 P + 1 +)
)+ c +
+R
c

)137
P + 2 ++)
) +
P 1 eR PHP c
+) + c P

thai_datetime
c c c ^
c^ .. c^ .. )
1. (thai_datetime)
)1=1
) +)
) P
2. 1 = 1 (set_datetime)
c
e+
c
3. =1 (cast_datetime)
c c
c

d m y ^
H M S )

4. = (short_month)

)
5. D (long_month)

)P
6. ==F (year)
7. == (month)
8. ==1 (day)
9. ==1 (hour)
10. == (minute)
11. == (second)

1. <?php
2. class thai_datetime {
3.
var $datetime;
4.
function thai_datetime($valuedate = NULL) {
5.
$this->set_datetime($valuedate);
6.
}
7.
function set_datetime($valuedate) {
8.
if(empty($valuedate))
9.
$this->datetime = date("Y-m-d H:i:s");
10.
else
11.
$this->datetime = $valuedate;
12.
}
13.
function cast_datetime($value, $type) {
14.
$value = strtotime($value);
15.
switch($type) {
16.
case "d": return date("j", $value);
17.
case "m": return date("m", $value);
18.
case "y": return date("Y", $value);
19.
case "H": return date("H", $value);
20.
case "M": return date("M", $value);
21.
case "S": return date("S", $value);
22.
}
23.
}
24.
function short_month($m_val = NULL) {
25.
if(empty($m_val))
26.
$m_val = $this->cast_datetime($this->datetime,"m");
27.
switch($m_val) {
28.
case 1: return ".."; break;
29.
case 2: return ".."; break;
30.
case 3: return ".."; break;
31.
case 4: return ".."; break;
32.
case 5: return ".."; break;
33.
case 6: return ".."; break;
34.
case 7: return ".."; break;
35.
case 8: return ".."; break;
36.
case 9: return ".."; break;
37.
case 10: return ".."; break;
38.
case 11: return ".."; break;
39.
case 12: return ".."; break;
40.
default : return ".."; break;
41.
}
42.
}
43.
function long_month($m_val = NULL) {
44.
if(empty($m_val))
45.
$m_val = $this->cast_datetime($this->datetime,"m");
46.
switch($m_val) {
47.
case 1: return ""; break;
48.
case 2: return ""; break;
49.
case 3: return ""; break;
50.
case 4: return ""; break;
51.
case 5: return ""; break;
52.
case 6: return ""; break;
53.
case 7: return ""; break;
54.
case 8: return ""; break;
55.
case 9: return ""; break;
56.
case 10: return ""; break;
57.
case 11: return ""; break;
58.
case 12: return ""; break;
59.
default : return ""; break;

60.
61.
62.
63.
64.
65.
66.
67.
68.
69.
70.
71.
72.
73.
74.
75.
76.
77.
78.
79.
80.
81.
82.}
83.?>

}
}
function year($value = NULL) {
if(empty($value))
return $this->cast_datetime($this->datetime,"y")+543;
else return $value+543;
}
function month() {
return ($this->cast_datetime($this->datetime,"m"));
}
function day() {
return $this->cast_datetime($this->datetime,"d");
}
function hour() {
return $this->cast_datetime($this->datetime,"H");
}
function minute() {
return $this->cast_datetime($this->datetime,"M");
}
function second() {
return $this->cast_datetime($this->datetime,"S");
}

+ 4
+ 4 P eR PHP
eR + +
+ + 5
1.
2.
3.
4.
5.
6.
7.

<?php
$x = new thai_datetime();
echo $x->day() , " " ,$x->short_month(), " " ,$x->year();
echo '<br />';
$y = new thai_datetime("2005-1-20");
echo $y->day() , " " ,$y->short_month(), " " ,$y->year();
?>

+ 5
11 .. 2550
20 .. 2548

2
+ 5 P + 2 + x y + x )
P ce+ (. ) + y )
c 20 1 ^ 2005 20 .. 2548
) )+ P )

c )
MySQL + c ) MySQL
+ P

PHP Manual, http://www.php.net, January 2007

Zend PHP Certification Study Guide, Zend Technologies, August 2004

Concepts of Programming Languages (7th Edition), Robert W. Sebesta, April 2005

You might also like