PHP

Initializing and Cleaning Up Objects

While I had the beginnings of a Product object type earlier, one thing about its usage that certainly seems suboptimal is the need to set the member data manually. To use it, I would have to write code along the following lines:

<?php
   $prod = new Product();
   $prod->name = "Super Fishing Pole 10b";
   $prod->desc = "This is the best fishing pole ever ...";
   // etc.
?>

What I would ideally like is a system in which I can pass the data about the product to the Product object as I create it so that from the moment of its creation, the object knows its properties and can verify that it has everything it needs.

To solve this problem, I introduce the concept of a constructor. This is a method that is executed whenever a new instance of the object type is created. The constructor can accept parameters that are passed via the call to the new operator by putting them in parentheses after the type name. If you do not write your own constructor, a default or blank one is provided for you that accepts no parameters. (This explains why the preceding sample code has empty parentheses after the name Product.)

<?php
  $prod = new Product();  // default constructor has no args.
?>

I will now define my constructor for the Product class. This constructor will require programmers to give us the necessary information when creating an object for a product. To do this in PHP, you define a method with the name __construct and make sure the keyword function is placed before it:

<?
  class Product
  {
    // member variables, etc....
    public function __construct
    (
      $in_prodid,
      $in_prodname,
      $in_proddesc,
      $in_price_pu,
      $in_location
    )
    {
      $this->id = $in_prodid;
      $this->name = $in_prodname;
      $this->desc = $in_proddesc;
      $this->price_per_unit = $in_price_pu;
      $this->location = $in_location;
    }
    // more methods and stuff follows
  }
?>

Now, when you create an instance of this class, you are obliged to pass in the required data:

<?php
  $prod = new Product($product_id,
                      $product_name,
                      $product_description,
                      $product_unit_price,
                      $product_supplier);
?>

If somebody tries to create an instance of the Product class without passing in the appropriate values to the constructor, PHP triggers a warning for each missing parameter.

Similarly, there exists a special method called a destructor that is called whenever my object is finally destroyed. I implement this in PHP via a function called __destruct. If I do not define one, PHP does its own cleanup and continues. For my Product class, there is not much cleanup required, but I can still put in a __destruct method just in case.

<?php
  class Product
  {
    // member declarations, constructors, etc.
    public function __destruct()
    {
      // put cleanup code here!!
    }
  }
?>

As mentioned earlier, the destructor for an object instance is not always called when you would expect. If PHP can easily determine there are no outstanding references left to an object, it immediately destroys the object and calls your destructor. However, in other situations, it may take until the end of the script to figure out that there are no references to an object that can be destroyed. If you are uncertain as to when your object will be destroyed, implement a destroy or clean_up method to call when you finish using an object.

by BrainBellupdated
Advertisement: