PHP DevCenter

oreilly.comSafari Books Online.Conferences.

We've expanded our LAMP news coverage and improved our search! Search for all things LAMP across O'Reilly!

Search
Search Tips

advertisement

Listen Print Subscribe to PHP Subscribe to Newsletters

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.

Programming PHP

Related Reading

Programming PHP
By Rasmus Lerdorf, Kevin Tatroe

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




Tagged Articles

Post to del.icio.us

This article has been tagged:

php

Articles that share the tag php:

Understanding MVC in PHP (477 tags)

The PHP Scalability Myth (123 tags)

The Dynamic Duo of PEAR::DB and Smarty (53 tags)

PHP Form Handling (43 tags)

Very Dynamic Web Interfaces (39 tags)

View All

Sponsored Resources

  • Inside Lightroom
Advertisement

Sponsored by:

O'Reilly Media

©2009, O'Reilly Media, Inc.
(707) 827-7000 / (800) 998-9938
All trademarks and registered trademarks appearing on oreilly.com are the property of their respective owners.
About O'Reilly
Academic Solutions
Authors
Contacts
Customer Service
Jobs
Newsletters
O'Reilly Labs
Press Room
Privacy Policy
RSS Feeds
Terms of Service
User Groups
Writing for O'Reilly
Content Archive
Business Technology
Computer Technology
Google
Microsoft
Mobile
Network
Operating System
Digital Photography
Programming
Software
Web
Web Design
More O'Reilly Sites
O'Reilly Radar
Ignite
Tools of Change for Publishing
Digital Media
Inside iPhone
O'Reilly FYI
makezine.com
craftzine.com
hackszine.com
perl.com
xml.com

Partner Sites
InsideRIA
java.net
O'Reilly Insights on Forbes.com