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.

PHP 5 Touching OOP

i read and i read and im confuse. ok next chapter.

PHP5 OO Language

wow now im actually learn Object Oriented thingy.

class Person
Method :
setName($name)
getName()

Properties :
$name

and this is the code :

class Person {
private $name;

function setName($name) {
$this->name = $name;
}

function getName() {

}
};

$judy = new Person();
$judy->setName("Judy");

$joe = new Person();
$joe->setName("Joe");

print $judy->getName() . "\n" ;
print $joe->getName() . "\n" ;

itll print the name.

PHP 5 First Touch

Right now the book called PHP5 Power Programming is helping me.
Itll let me learn few basic code which i prefer it that way and i hope i can create MySQL & PHP Application and other app that use PHP5.

im not reading any of chapter 1 but just go straight to chapter 2 that consist Basic Language.

this code is HELLO WORLD with HTML Embedding.

<html>
<head>Sample PHP Script</head>
<body>
The following prints "Hello, World"
<?php
print "Hello World" ;
?>
</body>
</html>

this will lead to this output
<html>
<head>Sample PHP Script</head>
<body>
The following prints "Hello, World"
"Hello World"

</body>
</html>

Comment
/*
* this is C way
*/

// this still is c way

# this is shell way

VARIABLES
its very different from C and Java you dont need to declare their variable before using them, you dont declare their type as result variable can change their type of its value as much as you want.

Starting to Learn Wordpress

 I'm gonna start learning wordpress again. I know it's been a while and I'm using Blogger to write everything.  I don't know...