ASPit - Totally ASP JSit - Totally JavaScript
Search PHPit

Use this textbox to search for articles on PHPit. Seperate keywords with a space.

Advertisements

PHP5 Class Basics, Part 1

(Page 1 out of 3)

Abstract

In this first part of a new article series on PHP5 classes and objects you will be introduced to the basic concepts of using classes, the constructor and destructor methods and class inheritance.

Introduction

With PHP5 objects have started to play a much larger role, even though PHP4 already had adequate support for objects, and you will find objects in any large or even small PHP application. And let's face it, objects are extremely useful and flexible at the same time, which is the reason why most of us use them.

But if you don't know much about classes and objects, then this is exactly the tutorial for you. I'm going to take you through the basics of classes and objects, and we'll discuss almost every feature of classes in PHP5. We'll start with the absolute basics, and that's the Class definition itself, and using classes.

Please note that this article is not about OOP (Object Orientated Programming), and will teach you very little about OOP. Although you will learn how to use objects in this tutorial, OOP is a whole different way of programming and requires a complete mind shift. Obviously that's more something for those who are already comfortable with objects and classes.

Let's get started with the basics of classes.

Basic Concepts

According to Wikipedia "A class describes a collection of encapsulated instance variables and methods (functions)", which is a very nice definition for a class. A class can have its own variables, called properties, and its own functions, called methods. The basic structure of a class looks like this:

Class MyCar {

        // Properties and methods go here
       
}

?>

We now have a fully functional class, although it doesn't do anything yet, and it doesn't hold any properties. Let's make our class do something. See the example below:

Class MyCar {

        function drive() {
                echo 'Vrooooom...';
        }
       
}

?>

We have now added a function to our class called drive(), which simple prints a message ("Vroooom…"). A function of a class is usually called a class method.

Our class is now possible of doing something, but we still can't use it, because we haven't created an instance of our class yet.

To be able to use the class, its methods and its properties, we will have to create an instance of the class, using the 'new' keyword. By making an instance of the class we create an object. See the example below:

Class MyCar {
        function drive() {
                echo 'Vrooooom...';
        }
}

$porsche = new MyCar();
$porsche->drive();

?>

(View Live Demo)

As you can see we used the 'new' keyword to create an instance of the MyCar class, and assigned it to the $porsche variable. We then called the drive() method of the MyCar class with the $porsche object.

It's important to note the difference between a class and an object. A class is the actual class, i.e. the 'MyCar' class. An object is an instance of a particular class, i.e. the $porsche variable is an object.

You can only create one 'MyCar' class, and if you try to create two classes called 'MyCar' you will get an error. On the other hand, you can create multiple instances of a particular class. See the example below:

Class MyCar {
        function drive() {
                echo 'Vrooooom...';
        }
}

// Instance 1
$porsche = new MyCar();
$porsche->drive();

echo '';

// Instance 2
$bmw = new MyCar();
$bmw->drive();

echo '';

// Instance 3
$volvo = new MyCar();
$volvo->drive();

echo '';

?>

(View Live Demo)

In the above example we have created three instances of the MyCar class, which means we have three objects, $porsche, $bmw and $volvo. Each object has its own drive() method.

Next: Constructor/Destructor »



One Response to “PHP5 Class Basics, Part 1”

  1. PHPDeveloper.org Says:

    PHPit.net: PHP5 Class Basics, Part 1…

Leave a Reply

About the author
Dennis Pallett is the main contributor to PHPit. He owns several websites, including ASPit and Chill2Music. He is currently still studying.
Article Index
  1. Introduction & Basic Concepts
  2. Constructor/Destructor
  3. Inheritance & Conclusion
Bookmark Article
Download Article
PDF
Download this article as a PDF file