in_array

Friday, July 24th, 2009

<?php
class Person {
    private $name;    
    private $age;    
    private $id;    

    function __construct( $name, $age ) {
        $this->name = $name;
        $this->age = $age;
    }

    function setId( $id ) {
        $this->id = $id;
    }
    
    function getId(){
        echo "get id method";    
    }
    
    function __clone() {
        $this->id = 0;
    }
}
$p = new Person("A",10);

$method = "getId";     // define a method name

if ( in_array( $method, get_class_methods( $p ) ) ) {
    print $p->$method();  // invoke the method
}

?>

           
       

Add a member variable to a class on the fly

Tuesday, July 7th, 2009

class C2: pass
C2.x = 23
print C2.x                    

           
       

abstract class demo

Saturday, June 27th, 2009

<?php
class Employee {
    private $title;
    private $lastName;
    private $firstName;
    protected $salary;
    private $ratio = 0; 
    
    public function __construct($title, $firstName, $mainName, $salary ) { 
        $this->title     = $title;
        $this->firstName = $firstName;
        $this->lastName  = $mainName;
        $this->salary     = $salary;
    }

    public function firstName() {
        return $this->firstName;
    }

    public function getlastName() {
        return $this->lastName;
    }

    public function setRatio( $num ) {
        $this->ratio=$num;
    }

    public function getRatio() {
        return $this->ratio;
    }
    
    public function getTitle() {
        return $this->title;
    }

    public function getSalary() {
        return ($this->salary - $this->ratio);
    }

    public function getFullName() {
        return "{$this->firstName}" . " {$this->lastName}";
    }

    function getSummaryLine() {
        $base  = "$this->title ( $this->lastName, ";
        $base .= "$this->firstName )"; 
        return $base;
    }
}

abstract class EmployeeWriter {
    abstract static function write( Employee $shopProduct );
}

class TextEmployeeWriter extends EmployeeWriter {
    static function write( Employee $shopEmployee ) {
        $str  = "{$shopEmployee->getTitle()}: ";   
        $str .= $shopEmployee->getFullName();
        $str .= " ({$shopEmployee->getSalary()})\n";
        print $str;
    }
}

$developer1 = new Employee("A", "A1", "A2", 5.99 );
TextEmployeeWriter::write( $developer1 );

?>

           
       

Inheriting a Shape Class

Friday, June 26th, 2009

<?php
     class shape {
          var $x;
          var $y;
   
          function shape()  {
               print("Shape constructor called <br />");
          }
          function get_x()  {
               return $this->x;
          }
          function get_y()  {
               return $this->y;
          }
          function set_x($x)  {
               $this->x = $x;
          }
          function set_y($y)  {
               $this->y = $y;
          }
          function move_to($x, $y)  {
               $this->x = $x;
               $this->y = $y;
          }
          function print_data()  {
               print("Shape is currently at " . $this->get_x() . ":" .
                                                $this->get_y() . "<br />");
          }
          function draw()
          {}
     }
     class rectangle extends shape
     {
          function rectangle($x, $y)  {
               $this->move_to($x, $y);
          }
          function draw()  {
               print("Drawing rectangle at " . $this->x . ":" .
                                               $this->y . "<br />");
          }
          function print_data()  {
               print("Rectangle currently at " . $this->get_x() . ":" .
                                                 $this->get_y() . "<br />");
          }
     }
     
     
     $rect1 = new rectangle(100, 100);
     $rect1->draw();
     $rect1->print_data();
?>