Вы находитесь на странице: 1из 2

Articles and Tutorials - PHP Classes And Objects: A Guide To Development Page 1 of 2


292 users

Start typing to search for PHP and
MySQL Code Snippets and Articles

Title : PHP Classes And Objects: A Guide To Development


Let me start off by explaining the differences between a class and an object. Many people look at these 2 things and think that they are the same thing. A class is NOT the same
thing as an object.

A simple comparison here. When you build a house you first create the blueprints, then you build the house from the blue prints. Now are the blueprints the same thing as a
house? No, they are just the design for the house. Think of a class as the blueprints for an object.

An object in simple terms is constructed from the class "blueprint".

Let's talk about classes first, there are 2 things that a class can contain, properties and methods. A property is a variable such as you use everywhere in your PHP applications.
$user is a variable. PHP4 and PHP5 are very different in how they handle their objects.

In PHP4 objects were passed by value, meaning every time you passed an object to a function or you did $var = $object it would copy the entire object. Each time you copied the
object you are using that much more memory. In PHP5 they fixed this problem and started passing objects by reference (or handle) The difference is this. When you create any
variable it is stored in a certain memory location, when you pass by reference PHP passes the memory location rather than the entire object. A reference to where the object is
located uses much less memory.

That is not the only difference between classes and objects in php5. We will cover more later.

Now you must be asking by now, how do you create a class?

A Basic Class Definition

class user {
var $name;
var $userID;

function user($uname, $uid) {

$this->name = $uname;
$this->userID = $uid;

The class definition above contains 3 things. It has 2 properties $name and $userID, it also has one method named user() (which is the constructor in this case).

You might be asking yourself what is the $this variable?

When writing classes you can use $this to access the current object's properties and methods.

in PHP when you create a class there are things such as constructors, these are methods that are run as soon as an object is instantiated/created. If you create a function inside
of a class that has the same name as the class, PHP will use that function as the class constructor. Notice how the class user definition doesn't contain () that is because when
you declare a class you cannot use them. Instead you should use a constructor, this will allow you to pass values to an object.

In PHP4 objects are referred to as array's on steroids. There was not much of a difference between the two, now that php5 has come out they have changed that. In PHP5 they
have added much more to the way objects are handled that PHP may be considered a true object oriented programming language.

Ok so how do I create an object?

Once you create the class "blueprint" for your object you can then instantiate it. The way this works is like so.

$user = new user("joe", 12);

Notice how I passed in the string "joe" and the integer 12, when the class is created, the constructor is run and the property values of the object are now set.

if you were to do

echo $user->name;

*** Notice how you can access the property using the object, this is a public property ***

it would print "joe"

What was added in PHP5?

First of all PHP5 added a new way to write constructors, when you create a method with the name __construct (that is 2 underscores before the word construct) it will use that as
the class constructor. What happens when you have a class named user, a method named user and a method named __construct? With PHP4 it would have used the method
user as the constructor, in PHP5 it ignores the method user and uses the method __construct, this will allow you to update your PHP4 code to PHP5 code.

PHP5 also added the ability to have deconstructors and as you may have guessed, these methods are run when the object is destroyed. You would generally use a destructor to
do "clean up" for your object, releasing any variables, etc.. that may be using up memory. The deconstructor is created just like the constructor method, that is 2 underscores
before the name destruct (__destruct)

Now on to some harder to grasp topics.

In PHP 4 as shown above all properties are automatically created to be of 'public' type, this means that when you use your object ($user) you can read/write any of the properties
in the class, this is why classes were referenced as array's on steroids. With the release of PHP5 they have added the ability to have 3 more types giving us a total of 4 types to
use in our classes. Please not you can use types for either properties or methods, you can have a private method.


public - this is the same as it was in PHP4 you can read/write properties by directly referencing them ($user->name = "joe";)

http://www.weberdev.com/Print-Article.php?ArticleID=405 30.07.2008
Articles and Tutorials - PHP Classes And Objects: A Guide To Development Page 2 of 2

private - This is a bit more tricky, a private type means that the method or property is only accessible from the current object itself. if you try to use $user->name = "joe"; and the
name property is designated private you will get an error. You cannot read or write private properties like that. When you have private properties you usually have a public method
that allows you to read/write the properties. Why would you do this and not just leave them public? You can better control how your properties and methods are accessed by using
private types.

A Public Method Getting Or Setting
A Private Property.

function getOrSetName($name) {
if($name) $this->name = $name;
else return $this->name;

protected - Protected members can be accessed by methods of the current class or any class that is extending the current class. Protected members do not allow child classes to
access the members directly. you would have to use parent::MethodName or parent::PropertyName to access protected members from a child class.

static - When you create an object and then destroy an object, most of the time all of the properties that you set are wiped clean and no longer store the information you had them
storing. Sometimes this is not the desired effect, sometimes you would like to retain a value even though an object has been destroyed. Static members do not relate to a
particular object but to the class itself.

Using a static property and method.

** The following class is from **

Core PHP Programming 3rd Ed.

ISBN: 013046369

class Counter {
private static $count = 0;

function __construct() {

function __destruct() {

static function getCount() {

return self::$count;

// create one instance

$c = new Counter();

// print 1
print(Counter::getCount() . "<br>\n");

// create another instance

$c1 = new Counter();

// print 2
print(Counter::getCount() . "<br>\n");

$c = NULL;

// print 1
print(Counter::getCount() . "<br>\n");

$c1 = NULL;

As you can see static properties and methods cannot be referenced using $this or the object itself but you can reference it by self or the class name such as Counter::getCount()
is using the class name, you use this method when you are not writing code within the class definition. When you are writing code in the class definition, use self::getCount()

This is as far as I am going to take this article, I will follow with more examples and with more articles pertaining to classes, this article was basically just to give you an idea of how
to use classes and objects.

Any questions just leave me some comments and I will bet back to you

http://www.weberdev.com/Print-Article.php?ArticleID=405 30.07.2008