Categories
PHP Programming

PHP Object type hinting

Introduced with PHP 5.1, it basically looks like this:

function setWriter( ObjectWriter $objwriter=null ) {
    $this->writer = $objwriter;
}

Where the $objwriter has to be either null or of the type ObjectWriter, or it will produce a runtime error. The =null part is optional.

Advantages:

Forces to pass the right kind of object to a method, saving us the trouble of testing before passing. It also makes the code clearer regarding method signature.

Disadvantage:

Since it only runs at runtime, if it is buried within conditional statements that only run on xmas, you may get a call on your holiday to get your budd to the office and fix it.

Not that you can’t use type hinting with primitives.

Categories
PHP

PHP abstract classes

Basically, abstract classes define methods but don’t have any functionality inside of them, and can’t be instantiated directly. They are just blueprints for classes that extend them, inheriting their methods (and being forced to define them). Abstract classes can’t be instantiated, just extended.

Categories
PHP

PHP static methods

A static class in PHP acts pretty much the same way as static variables, it is loaded into the “static” space, where every time it is spawned or instantiated, it is the same object already built before.

Static variables inside classes can’t be addressed with the $this identifier:

class SomeClass {
   public static $counter = 0;
   function __construct() {
        self::$counter++; // Notice how we say self::$counter instead of $this->counter
   }
}
Creating a static method inside a class:
class SomeClass {
   public static function do() {
        // Code.
   }
}
So now, to call the method, we need to do the following:
SomeClass::do(); // $obj->do(); // NO!
Constants are like static variables, except that their values is final and can't be changed:
class SomeClass {
   const PI = 3.14;
}
To call the value:

$obj::PI; // $obj->PI // !No

 
Advantages / reasons for using static classes:

  1. Available anywhere from your script, without the need to instanciate an object or store it into a global variable.
  2. It can save you performance time, not instanciating objects just to access their static methods, functionality or variables.
Categories
PHP

PHP using the class scope resolution operator ::

Since it is possible methods will have the same name or signature accross multiple classes, the way to specify which class the method you are calling belongs to is:

ClassName::method_name();

Variations of this are:

  • parent::method_name(); // Calling the class parent method
  • self::method_name(); // Same as $this->method_name();

In PHP4, you could call any method using this syntaxis, in PHP5, since now we have visibility attributes, you can only call static functions with it.

So the syntaxis above is used to call overriden parent methods, instead of the current class ones, for instance: $base = parent::getSummaryLine(); to call the parent getSummaryLine() method, not the overriden version of it in this object.

Categories
PHP

PHP extending classes, class inheritance

Sample code:

class ChildClass extends ClassName { }

In this case ClassName is the parent, or superclass, and ChildClass inherits all of its properties, plus it is able to add its own.

When you use inheritance, you are responsible to passing arguments back to your parent class. Take the following example (check how parent::__construct() takes care of that for you):

function __construct(  $title, $firstName,
                           $mainName, $price, $numPages ) {
        parent::__construct(  $title, $firstName,
                              $mainName, $price );
        $this->numPages = $numPages;
    }

It is actually better to use parent::__construct() instead of real_parent_class_name_here::__construct(), just in case the parent changes later you don’t have to worry about changing code in the child class.

Categories
PHP

PHP __autoload()

__autoload() runs (on PHP5) when a class that has not been included is requested. Usually it looks like this:

function __autoload ($class) {
   require_once($class . ‘.php’);
}

Categories
PHP

PHP Classes

A class is a code template used to generate objects. It is an abstract definition of an object that has properties and methods. It stores data and do tasks.

A class blueprint in PHP looks like this:

class ClassName {
   public $var1, $var2;
   // NOTE: there is no functionality in the class itself, only declarations
   // so you couldn't do something here like: public $var3 = $var1 + $var2;
   function __construct() {
         // This function runs automatically as the object is created
         // Connect to database, set cookies, initialize stuff here
   }
   function __destructor(){
      // Runs automatically when unset() is applied to the object. Destructors can't take arguments
   }
   public function function_name() {
        // Function code.
   }
}