interesting example, for comparison (e.g. in TextWrangler (OSX)):
procedural:
[cc lang=”php” escaped=”true” width=”500″]
‘Tom’,
‘job’ => ‘Button-Pusher’,
‘age’ => 34
);
$person2 = array(
‘name’ => ‘John’,
‘job’ => ‘Lever-Puller’,
‘age’ => 41
);
// Output the starting values for the people
echo ”
Person 1: ", print_r($person1, TRUE), "
“;
echo ”
Person 2: ", print_r($person2, TRUE), "
“;
// Tom got a promotion and had a birthday
$person1 = changeJob($person1, ‘Box-Mover’);
$person1 = happyBirthday($person1);
// John just had a birthday
$person2 = happyBirthday($person2);
// Output the new values for the people
echo ”
Person 1: ", print_r($person1, TRUE), "
“;
echo ”
Person 2: ", print_r($person2, TRUE), "
“;
?>
[/cc]
object oriented:
[cc lang=”php” escaped=”true” width=”500″]
_name = $name;
$this->_job = $job;
$this->_age = $age;
}
public function changeJob($newjob)
{
$this->_job = $newjob;
}
public function happyBirthday()
{
++$this->_age;
}
}
// Create two new people
$person1 = new Person(“Tom”, “Button-Pusher”, 34);
$person2 = new Person(“John”, “Lever Puller”, 41);
// Output their starting point
echo ”
Person 1: ", print_r($person1, TRUE), "
“;
echo ”
Person 2: ", print_r($person2, TRUE), "
“;
// Give Tom a promotion and a birthday
$person1->changeJob(“Box-Mover”);
$person1->happyBirthday();
// John just gets a year older
$person2->happyBirthday();
// Output the ending values
echo ”
Person 1: ", print_r($person1, TRUE), "
“;
echo ”
Person 2: ", print_r($person2, TRUE), "
“;
?>
[/cc]
liked this article?
- only together we can create a truly free world
- plz support dwaves to keep it up & running!
- (yes the info on the internet is (mostly) free but beer is still not free (still have to work on that))
- really really hate advertisement
- contribute: whenever a solution was found, blog about it for others to find!
- talk about, recommend & link to this blog and articles
- thanks to all who contribute!