PHP DevCenter
oreilly.comSafari Books Online.Conferences.

advertisement


PHP Foundations Introduction to PHP Objects, Part 1

by John Coggeshall
07/18/2002

Introduction

Welcome back! Last time we met, I discussed with you the concept of using multiple files to store your PHP scripts.

In today's column, I'll be introducing one of the more interesting and useful methods of writing PHP scripts -- the object-oriented method. Those of you who might have experience with an object-oriented programming language such as Java or even C++ will find themselves right at home with most of the concepts I'll begin outlining here. However, be warned -- PHP objects have a mixture of C++ and Java, making them a little different than what you might be used to. In any case, let's get started with the basics.

The What and Why of Objects

For those of you without much experience in object oriented programming, you may be asking yourself, what exactly is an object and why is it useful? The best answer that I have heard describing an object is simply a "black box" that serves a function or functions. What is a "black box?" What I mean is that a "black box" is a chunk of code which is completely independent of any other code -- it takes in information, processes it, and returns a result. You, as the developer, do not need to know anything whatsoever regarding the internal workings of the code. Furthermore, objects are extendable, allowing developers to build off of already-existing code.

I know all of the above might be a lot to take in at once (and we haven't even discussed everything yet). Don't worry! I'll be discussing more on the things I've touched on above in the future, when it'll make much more sense. Thankfully, there is a lot that can be understood just by being walked through the basic syntax, so let's take a look.

Simple Class Syntax in PHP

The basic syntax in PHP object-oriented programming is basically the same as in a language such as Java. Classes (the code that creates an "object") are defined by the class keyword. Here's an example of a class with nothing inside of it:

<?php

class empty {

/* Member functions and variables go here */

}

?>

Member Variables and Creating an Instance

Of course, a class that has nothing inside of it isn't useful. For a class to be of any use, we'll need to add some functions and member variables. Let's start by adding the variable $foo to our class, renaming the class to foo_object, and, just to make things interesting, let's also set the value of $foo to the string value bar:

<?php

class foo_object {

var $foo = 'bar';

}

?>

Now that we have a class with something inside of it, how do we actually get at and work with this data? To create an object (also known as an instance of a class) you'll need to be introduced to another keyword -- the new keyword. Here's an example where we'll create a new object $myobject that is an instance of our example class foo_object:

<?php

$myobject = new foo_object();

?>

When the PHP engine encounters this statement, it searches for a definition of the foo_object class. Since we have already defined it, the engine creates a working copy of this class and stores the reference to it in the variable $myobject. This means that through the variable $myobject, you can now access any variables or functions that existed within the definition of the foo_object class. For instance, if we wanted to work with the $foo variable inside of this particular instance of foo_object, we could do so as follows:

<?php

            // Set the value of the $foo variable inside of

            // $myobject to 5

$myobject->foo = 5;

// Echo the value of the $foo variable

echo $myobject->foo; // Prints '5' to the browser

?>

Pages: 1, 2

Next Pagearrow




Valuable Online Certification Training

Online Certification for Your Career
Earn a Certificate for Professional Development from the University of Illinois Office of Continuing Education upon completion of each online certificate program.

PHP/SQL Programming Certificate — The PHP/SQL Programming Certificate series is comprised of four courses covering beginning to advanced PHP programming, beginning to advanced database programming using the SQL language, database theory, and integrated Web 2.0 programming using PHP and SQL on the Unix/Linux mySQL platform.

Enroll today!


Sponsored by: