osCommerce Home | osCommerce eBook Store | osCommerce Tutorials

PHP Class Constructor Basic Tutorial

osCommerce store uses quite a lot of PHP class constructor. Therefore this is better to have some ideas what is PHP class constructor and how it works.

A PHP class constructor function (__construct) is similar to other functions. The main differences from the other functions is that a constructor method is used within a class body and will be initiated or activated when a new instance is created.

PHP class constructor is sometimes hard to explain how it works. In brief you may consuder a PHP class constructor is an object. Once you create it, you can use it again and again.

Let's take some basic examples of how PHP class constructor works.

PHP Class Constructor Example 1:

<?php

// Categories class
class Categories {
     // Declare variables
     var $title;

     function showCategories( ) {
     	//Use $this to access or point to the $title variable
     	//Then assign a value to this element
     	$this->title = "Hardware";
	}
     
     //use the __construct() function 
     // begins with two underscore "__"
     function __construct () {
	 //Use $this to access or point to the showCategories() method
	 $this ->showCategories( );
     }

}

// Create a new instance of the class Categories so that we can use it.
$categoriesName = new Categories();

// Output the title
// The showCategories() method will be executed 
echo "The Title is: " . $categoriesName -> title;

?>

The output of the above typical PHP class constructor example should look like:

The Title is: Hardware

The basic structure of a PHP class constructor can be illustrated with the diagram below:

PHP class constructor

PHP Class Constructor Example 2:

Let's add more variables.

<?php

// Categories class
class Categories {
     // Declare variables
     var $title;
var $description; function showCategories( ) { //Use $this to access or point to the $title variable //Then assign a value to this element $this->title = "Hardware";
$this->description = "Computer keyboard"; } //use the __construct() function // begins with two underscore "__" function __construct () { //Use $this to access or point to the showCategories() method $this ->showCategories( ); } } // Create a new instance of the class Categories so that we can use it. $categoriesName = new Categories(); // Output the title // The showCategories() method will be executed echo "The Title is " . $categoriesName -> title . " and Description is " . $categoriesName -> description; ?>

The output of PHP class constructor example 2 will be:

The Title is Hardware and Description is Computer keyboard

PHP Class Constructor Example 3:

Like functions, you can also pass variables to class constructor function.

<?php

// Categories class
class Categories {
     // Declare variables
     var $title;
var $description; var price; function showCategories( ) { //Use $this to access or point to the $title variable //Then assign a value to this element $this->title = "Hardware";
$this->description = "Computer keyboard"; $this->price= 100; } //use the __construct() function // begins with two underscore "__" function __construct () { //Use $this to access or point to the showCategories() method $this ->showCategories( ); } } // Create a new instance of the class Categories so that we can use it. // Also pass a variable to class constructor function $categoriesName = new Categories(100); // Output the title // The showCategories() method will be executed echo "Title: " . $categoriesName -> title . ". Description: " . $categoriesName -> description .
". Price: " . $categoriesName -> price; ?>

The output of PHP class constructor example 3 will be:

Title: Hardware. Description: Computer keyboard. Price: 100

Remarks

In osCommerce store, they use class constructor in a different way. They use a same function name as the class name, for example:

<?php

// Categories class
class Categories {
     var $title;

     
     // Use a function name same as the class name
     // This is same as __constructor()
     function Categories () {
	      $this ->showCategories( );
     }

}

A function name that inside a class object will act in the same way as a constructor function. It will also be initiated when a new instance is created. However I myself never recommend using this method as this is not a "normal" way. This may possibly be not allow to use in future PHP versions.

Now you should have some basic ideas how a class constructor works.