Contact Our Development Team
Free Code Tutorials & Open Source Code
Classes
Tutorials > PHP > Classes
What Are Classes?
A class is a programmatic description of an object. Classes may contain variables (referred to as properties when within a class) and functions (called methods). Let's take a look at the creation and use of a simple class:
<?php
    class Pet {
        public $type;
        public $age;
        public $name;
        
        function aboutMe() {
            echo("I am a " . $this->age . "-year-old " . $this->type . ". My name is " . $this->name . ".");
        }
    }
    
    $myPet = new Pet();
    $myPet->type = "dog";
    $myPet->age = 5;
    $myPet->name = "Snoopy";
    $myPet->aboutMe();
?>
First, we create the class by specifying its name after the class keyword. The entire class - its properties and methods - are enclosed within a set of braces. Here we have a simple class describing a pet, with three properties ($type, $name and $age), and one method, aboutMe(), which displays information about that pet.
 
A class is like a blueprint - it defines what our Pet is, and what it can do. Having designed the class, we then create an instance of that class using the new keyword. Any number of instances can be created in the same way, each one having its own type, age and name. Properties and methods of a class instance are accessed using the -> syntax. Having created a new Pet, we set its properties, then call its aboutMe() function with the following result:
I am a 5-year-old Dog. My name is Snoopy.
The $this Keyword
You'll notice several occurrences of the $this keyword in the above code. $this is a special variable used within an instance of a class to refer to itself. In the example above, we need the aboutMe() function to refer to the instance variables.
Visibility
Properties are declared with one of three keywords, public, protected or private - in the example above, all the properties are public. This means that they can be accessed by any part of the code. Properties declared as private may only be accessed by functions within the class instance itself. If we had defined $age as private, for example, the line $myPet->age = 5; would have produced an error.
 
The protected keyword is similar to private, but also allows the property to be accessed by parent classes and inherited classes. See the section on Inheritance for more information.
 
Class methods are public by default. However, we can specify the visibility explicitly:
private function doSomething() {
    //This function can only be accessed by other functions within the class.
}
Constructors and Destructors
Constructors and destructors are special functions which are run automatically when an instance of a class is created and destroyed respectively. A constructor is used for performing initial setup of a new instance - for example, setting the values of instance variables, opening network connections, etc. The destructor is then used at the end of the instance's life to free up any resources that that instance used - closing network connections or files, and releasing shared memory. Constructors in PHP5 should be named __construct, and destructors must be named __destruct. Let's look at an example of both in use:
<?php
        class ConstructorClass {
                function __construct() {
                        echo("__construct() called.<br>");
                }

                function __destruct() {
                        echo("__destruct() called.<br>");
                }
        }

        echo("Creating \$firstInstance.<br>");
        $firstInstance = new ConstructorClass();
        echo("Creating \$secondInstance.<br>");
        $secondInstance = new ConstructorClass();

        echo("Setting \$firstInstance to null.<br>");
        $firstInstance = null;
        echo("Ending script.<br>");
?>
Creating $firstInstance.
__construct() called.
Creating $secondInstance.
__construct() called.
Setting $firstInstance to null.
__destruct() called.
Ending script.
__destruct() called.
So what's happening? Each time the new keyword is used to create a new instance of the class ConstructorClass, PHP automatically calls the __construct() function within that class. the __destruct() function is called when that class instance is no longer accessible. When we set $firstInstance to null, the instance of ConstructorClass which that variable referred to can no longer be accessed, and PHP calls __destruct() on that instance. Finally, when the script ends, PHP calls the destructor of any class instances still remaining - in this case, $secondInstance.
Page Responses
Currently there have been no responses to this page...
If you have anything to contribute to this tutorial, found a bug, or know a better way of achieving the same goal, please leave your response below.
     
Copyright ©2009, Wired IDS Ltd. | Licensed under Creative Commons Attribution Share-Alike | Load time: 0.3052 seconds