WeberDev.com PHP and MySQL Code

LOG IN
BEGINNER GUIDES  |  PHP CLASSES  |  CODE SEARCH  |  ARTICLES SEARCH  |  PHP FORUMS  |  PHP MANUAL  |  PHP FUNCTIONS LIST  |  WEB SITE TEMPLATES
Start typing to search for PHP and MySQL Code Snippets and Articles Search
Submit a code Example / Snippet Submit Your Code
Search Engine Optimization Monitor SEO Monitor
Web Site UpTime Monitor UpTime Monitor
WeberDev's Monthly code contest PHP Code Contest
Your Personal Examples List My Favorite Examples
Your Personal Articles List My Favorite Articles
Edit Account Info Update Your Profile
PHP Code Search
Web Development Forums
Learn MySQL Playing Trivia
PHPBB2 Templates
Web Development Index
Web Development Resources
Web Development Content
PHPClasses
PHP Editor
PHP Jobs
Vision.To Design
Ajax Tutorials
PHP Programming Help
PHP/MySQL Programming
Webmaster Resources
Webmaster Forum
XML meta language
website builder
Mobile Dev World

Go Back Add a Comment Send this Article to a friend Add this Article to your personal favoritest for easy future access to your favorite Code Examples and Articles. Submit a code example Print this code example.
BACK ADD A COMMENT SEND TO A FRIEND ADD TO MY FAVORITES SUBMIT AN ARTICLE PRINT
Title : PHP Classes And Objects: A Guide To Development
Categories : PHP, PHP Classes, Object Oriented, Beginner Guides
Joe Crawford Jr.
Joe Crawford Jr.
Date : 2004-05-26
Grade : 3 of 5 (graded 1 times)
Viewed : 22832
Search : More Articles by Joe Crawford Jr.
Action : Grade This Article
Tools : My Favotite Articles


  Submit your own code examples 
 


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
private
protected
static

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

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() {
self::$count++
}

function __destruct() {
self::$count--;
}

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









PHP 101 Part 7 of 15 : The Bear Necessities
Categories : PHP, Beginner Guides, Object Oriented, PHP Classes
Building An Extensible Menu Class
Categories : PHP, PHP Classes, Object Oriented, Navigation
PHP and OOP
Categories : PHP, Object Oriented, Beginner Guides
PHP5: Designing And Using Interfaces
Categories : PHP, Object Oriented, Interfaces, PHP Classes, Security
PHP 101 Part 8 of 15 : Databases and Other Animals
Categories : PHP, Beginner Guides, Databases
Making PHP Forms Object-Oriented
Categories : PHP, HTML and PHP, Object Oriented
Beginners Guide to PHP - Introduction to cookies
Categories : Beginner Guides, Cookies, To PHP, PHP
PHP 101 Part 9 of 15 : SQLite My Fire!
Categories : PHP, Beginner Guides, Databases, SQLite
How TO Install PHP, Apache and MySQL on Linux or Unix
Categories : PHP, MySQL, Apache, Installation, Beginner Guides
Beginners guide to PHP and MySQL
Categories : PHP, Beginner Guides, Databases, MySQL, Installation
Counting - Creating a GIF based counter using PHP and MySQL
Categories : Beginner Guides, PHP, To PHP, To MySQL, MySQL
Building a Generic RSS Class With PHP
Categories : PHP, XML, Rich Site Summary (RSS), PHP Classes
PHP 101 Part 10 of 15 : A Session In The Cookie Jar
Categories : PHP, Beginner Guides, Cookies, Sessions
Counting - Creating a more sophisticated GIF based counter using PHP and MySQL
Categories : Beginner Guides, MySQL, PHP, To PHP, To MySQL
Creating a Credit Card Validation Class With PHP
Categories : PHP, Credit Cards, PHP Classes