Thursday, May 13, 2010

OOP with PHP 5.x

OOP
http://www.scribd.com/doc/38423/Object-Oriented-Programming-in-PHP-5
  • Abstraction
  • Modularization
  • Ecapsulation
  • Extensibility
Basic

class MyClass {
    public $data;
    private $private;
    static $static = 1;
    const constant = 'value';
    public function __construct() {
    }

    public function __destruct() {
    }

    protected function Action() {
    }

    private function Action() {
    }

    public function show() {
    }
}

Extending Class

class MyClass {
   public $data;
   public function __construct() {
       $this->data = 'Hello World";
   }



class Second extends MyClass {
   private $some;
   public function __construct() {
       $this->some = 'Second!';
       parent::__construct();
   }

}

Interface & implements
keyword implements:
  • create class based on interface;
  • can be combined with extends

interface Template {
   public function foo($var);
}

class MyClass implements Template {
   public function foo($var) {
       echo $var;
   }

}
class MyClass extends foobar implements Template {
   public function foo($var) {
       echo $var;
   }

}

Understanding PHP Object Model
http://www.slideshare.net/sebastian_bergmann/understanding-the-php-object-model




No comments:

Post a Comment