Wednesday, February 27, 2008

PHP 5 other OOP Code

other oo code

class Cat {
function miau() {
print "miau";
}
};

class Dog {
function wuff() {
print "wuff";
}
};

function printTheRightSound($obj) {
if ($obj instanceof Cat) {
$obj->miau();
} else if ($obj instanceof Dog) {
$obj->wuff();
} else {
print "Error: Passed wrong kind object";
}
print "\n";
}

printTheRightSound(new Cat());
printTheRightSound(new Dog());

//end class

the output is

miau
wuff

Inheritance is performed by using the [extends]

keyword :
class Child extends Parent {
//....
}

This how you would rewrite the previous example

using inheritance :
class Animal {
function makeSound() {
print "Error : This Method should be re-

implemented ins the children";
}
}

class Cat extends Animal {
function makeSound() {
print "miau";
}
}
class Dog extends Animal {
function makeSound() {
print "wuff";
}
}

function printTheRightSound($obj) {
if ($obj instanceof Animal) {
$obj->makeSound();

} else {
print "Error : Passed wrong kind of object";
}
print "\n";
}

printTheRightSound(new Cat());
printTheRightSound(new Dog());
\\end class

THe output is

miau
wuff

and this is the summary that i read its really

cute

"if youre new to PHP but have written code in

object oriented language,
youll probably not understand how people managed

to write object oriented code until now.
if youve writeen object oriented code in PHP4,
you were probably just dying for these new

features"

so what if im really new to object oriented

language.
itll probably i can never understand what should

i do with it.

i read and read and im done with this im off.
i peek in chapter 4 and its an advanced

OOP.....okay !-_-.
i think i need codes that written using OOP

techinque and lots of them so i can get the hang

of it.
in chapter 5 itll be Web Application.

No comments: