Prev | Current Page 18 | Next

Hasin Hayder

"Object-Oriented Programming with PHP5"


Dissection of an Object
So what is an object? Well, it's nothing but a piece of code with a bunch of properties
and methods. So is it similar to an array, as arrays can store data identified by
properties (well, they are called keys)? Objects are much more than arrays because
they contain some methods inside them. They can either hide them or expose them,
which are not possible in arrays. The object is somewhat comparable with a data
structure, data structure, and can incorporate a lot of other objects in itself and either
creates a tight coupling among them or a loose one. And object can incorporate a lot
of other object in itself and either creates a tight coupling among them or a loose one.
We will learn more about loose coupling and tight coupling later in this book and
understand how they will be useful for us.
Let's see the code of an object in PHP. The following object is a very simple
object which can send email to a bunch of users. In PHP5, objects are a lot more
different than an object in PHP4. We will not discuss the details of it, this is just an
introductory object to see how the objects are written in PHP.
//class.emailer.php
class emailer
{
private $sender;
private $recipients;
private $subject;
private $body;
function __construct($sender)
{
$this->sender = $sender;
$this->recipients = array();
}
public function addRecipients($recipient)
{
array_push($this->recipients, $recipient);
}
???
OOP vs.


Pages:
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30