PHP Class

Constructor

<?php
  class Bird {
    // Constructor
    function __construct($name, $breed, $price){
      $this->name = $name;
      $this->breed = $breed;
      $this->price = $price;
    }
 
    function testAll(){
      $this->testConstructor();
    }
 
    function testConstructor(){
      // Usage
      $tweedy = new Bird('Tweedy','canary',15);
      echo $tweedy->name;
    }
  }
  $bird = new Bird();
  $bird->testAll();
?>

Constructor with Default Values

<?php
  class Bird {
    // Constructor with default values
    function __construct($name='No Name', $breed='Unknown', $price=15){
      $this->name = $name;
      $this->breed = $breed;
      $this->price = $price;
    }
 
    function testAll(){
      $this->testConstructor();
    }
 
    function testConstructor(){
      // Usage
      $unknown = new Bird();
      echo $unknown->name;
    }
  }
  $bird = new Bird();
  $bird->testAll();
?>

Destructor

//-------- Destructor
public function __descruct(){
  $this->saveInfo();
}

Magic Methods

__construct() 
when new object is created
__destruct()
When object is destroyed
e.g. unset($instance) or end of script
__destroy() 
__autoload()
Refer to the class for the first time
Call constructor
Call static methods
__clone()
__get() and __set()
When property being get or set is not found 
__call()
When method being invoked is not found
__sleep()
When serialized
__wakeup()
When unserialized
__toString()

Exception Handling

try
{
  perform_some_action();
  if($some_action_results_in_error)
    throw new Exception("Houston, we've got a problem...");
 
  perform_another_action();
  if($other_action_results_in_error)
    throw new Exception("Houston, we've got a different problem...");
}
catch Exception $e
{
  handle_exception($e);
}

Member Visibilities

Public (default)
private
protected
 
class Bird {
  //------- Variables
  private $name;
  private $breed;
  private $price;
}

Static Member and self Keyword

<?php 
  class ODebug {
    //------- Helper methods
    public static function debug($msg, $indent=0){
      $indentStr = "";
      for ($i = 0; $i < $indent; $i++){
        $indentStr = $indentStr.'&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;';
      }
      echo $indentStr.$msg.'<br/>';
    }
 
    public function testDebug(){
      // self means current class; no $ and always ::
      self::debug('Test debug message');
    }
  }
 
  $odebug = new ODebug();
  $odebug->testDebug();
  ODebug::debug('Test debug',2);
?>

Constants

<?php 
  class ODebug {
    // Define a constant. 
    // No access modifier allowed. Default to public static
    const INDENT_CHAR='&nbsp;&nbsp;&nbsp;';
 
    //------- Helper methods
    public static function debug($msg, $indent=0){
      $indentStr = "";
      for ($i = 0; $i < $indent; $i++){
        $indentStr = $indentStr.self::INDENT_CHAR;
      }
      echo $indentStr.$msg.'<br/>';
    }
 
    public function testDebug(){
      self::debug('Test debug message');
    }
  }
 
  $odebug = new ODebug();
  $odebug->testDebug();
  ODebug::debug('Test debug',2);
?>

Class Inheritance:
Java like
-Single inheritance
-Multiple interfaces

<?php
  require_once('Bird.php');
  require_once('ODebug.php');
  class Tweedy extends Bird{
    public function chirp(){
      ODebug::debug("Tweedy tweedy...");
    }
  }
 
  class Parrot extends Bird{
    public function __construct($name){
      parent::__construct($name,"Parrot",50);
    }
 
    public function chirp(){
      ODebug::debug("Qwark Qwark...");
    }
  }
  $tweedy = new Tweedy('Tweedy2','Still Canary',30);
  $tweedy->chirp();
  $tweedy->display();
 
  $parrot = new Parrot("Patty");
  $parrot->chirp();
  $parrot->display();
?>

Abstract and Final Modifier
abstract public function birdCall();
final public function birdCall();
Interfaces
-Can only declare public functions
-Can not declare variables

<?php 
  interface IPet {
    public function getName();
    public function getBreed();
  }
 
require_once('IPet.php');
class Bird implements IPet{
    public function getName(){
        return $this->name;
    }
    public function getBreed(){
        return $this->breed;
    }
?>

Reflection API

class_exists($class,$autoload=true)
Class_implements($class)
Class_parents($class)
Get_class_methods($class)
Get_class_vars($class)
get_class($instance)
Get_declared_classes()
Get_declared_interfaces()
Get_object_vars($instance)
Get_parent_class($class)
Interface_exists($interface,$autoload=true)
Is_a($instance, $class)
Method_exists($instance, $method)
This entry was posted in php. Bookmark the permalink.