interesting example, for comparison (e.g. in TextWrangler (OSX)):
procedural:
<!--?php function changeJob($person, $newjob) { $person['job'] = $newjob; // Change the person's job return $person; } function happyBirthday($person) { ++$person['age']; // Add 1 to the person's age return $person; } $person1 = array( 'name' =--> 'Tom',
'job' => 'Button-Pusher',
'age' => 34
);
$person2 = array(
'name' => 'John',
'job' => 'Lever-Puller',
'age' => 41
);
// Output the starting values for the people
echo "
<pre>Person 1: ", print_r($person1, TRUE), "</pre>
";
echo "
<pre>Person 2: ", print_r($person2, TRUE), "</pre>
";
// 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 "
<pre>Person 1: ", print_r($person1, TRUE), "</pre>
";
echo "
<pre>Person 2: ", print_r($person2, TRUE), "</pre>
";
?>
'job' => 'Button-Pusher',
'age' => 34
);
$person2 = array(
'name' => 'John',
'job' => 'Lever-Puller',
'age' => 41
);
// Output the starting values for the people
echo "
<pre>Person 1: ", print_r($person1, TRUE), "</pre>
";
echo "
<pre>Person 2: ", print_r($person2, TRUE), "</pre>
";
// 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 "
<pre>Person 1: ", print_r($person1, TRUE), "</pre>
";
echo "
<pre>Person 2: ", print_r($person2, TRUE), "</pre>
";
?>
object oriented:
<!--?php class Person { private $_name; private $_job; private $_age; public function __construct($name, $job, $age) { $this--->_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 "
<pre>Person 1: ", print_r($person1, TRUE), "</pre>
";
echo "
<pre>Person 2: ", print_r($person2, TRUE), "</pre>
";
// 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 "
<pre>Person 1: ", print_r($person1, TRUE), "</pre>
";
echo "
<pre>Person 2: ", print_r($person2, TRUE), "</pre>
";
?>
$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 "
<pre>Person 1: ", print_r($person1, TRUE), "</pre>
";
echo "
<pre>Person 2: ", print_r($person2, TRUE), "</pre>
";
// 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 "
<pre>Person 1: ", print_r($person1, TRUE), "</pre>
";
echo "
<pre>Person 2: ", print_r($person2, TRUE), "</pre>
";
?>