Objects and Classes
http://www.mlinux.org/
by Keith Geffert

Data Encapsulation

      Object oriented programming (OOP) is one of the newest type of programming methods that has changed application design in just about all areas. Objects are entities that encapulate code and data in a nice little package. Imagine if all your variables of a certain type (class type) had built-in functions for doing things. Instead of taking a member record and translating its member email address to html printable characters you just asked the variable to automatically convert it that way when you ask for it? Now objects can be very complicated and we are not going to even attempt to show you how they work, but since objects do exist in PHP we want you to be aware of what they can accomplish.

Imagine you have all your member information in a database, and you need an easy way of updating and retrieving such info. Objects turn ordinary variables into super handy programming creatures. As an example....

/* get a new user from the database we know thier username is "bob" */

$user = new member('bob');
What did we just do? whatever you do. Don't touch the red button! We just created a new object. Now somewhere in our code (already written) the member object when it builds itself calls a special function called a constructor that builds the object and initializes all the special data it can carry. This constructor would see the username 'bob' and connect to our database and get bob's record and the populate itself with bobs information. Now the object $user is really bob!

So lets get some information out of $user describing the user "bob". Then lets change something in the object.
/* Let's get bob's full name */
$full_name = $user->show_full_name();      /* call method (object function) */

/* Let's change bob's email address */
$user->set_email_address("Bob Roberts <br@mlinux.org>");
The first statement puts the users full name into the $full_name variable. It does this by calling a special built-in function to the object $user called show_full_name(). This function is referred to as an object method. Methods do many things, and are usually used to operate on the data that the object contains, including storing and retrieving it. This is what show_full_name() does. It takes the full name of the user, and returns it as a string. What you don't know is how the data is stored in the object or where. You don't need to know just as long as the object provides an interface for you to retrieve it.

The second line calls a method set_email_address() with one argument. This argument is probably the replacement email address. What you don't see is that this object after storing the new email address in some data location calls an internal (non public) method to make the necessary changes in the database.

.... it was someting about a million monkeys pounding on a million keyboards with some reference to emacs.....





Previous       Next