Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Object Oriented Programming in PHP5

Avatar for djsipe djsipe
February 18, 2012

Object Oriented Programming in PHP5

An overview of some of the basics for developing object oriented code in PHP5.

Avatar for djsipe

djsipe

February 18, 2012
Tweet

More Decks by djsipe

Other Decks in Technology

Transcript

  1. Before We Get Started What is Object Oriented Programming? How

    Does PHP Implement these Concepts? Encapsulation Encapsulation Visibility Inheritance Polymorphism Abstraction Magic Methods Common Design Patterns
  2. What is Object Oriented Programming? Objects are instances of classes.

    Classes define how their instances (objects) will instances (objects) will operate. Objects can contain “private” information and methods. Classes can inherit from other classes
  3. Encapsulation classA { const C = 5; var $aVariable; function

    __construct() function __construct() { } function __destruct() { } function doSomething() { } }
  4. Visibility class A { const C = 5; protected $aVariable;

    public $bVariable; private $cVariable; private $cVariable; protected function __construct() { } private function __destruct() { } public function doSomething() { } }
  5. Inheritance class A { const C = 5; protected $aVariable;

    public $bVariable; private $cVariable; protected function __construct() { } private function __destruct() { } public function doSomething() { } } class B extends A { public function doSomething() { parent::doSomething(); } }
  6. Polymorphism “ “Polymorphism describes a pattern in Polymorphism describes a

    pattern in object oriented programming in which object oriented programming in which object oriented programming in which object oriented programming in which classes have different functionality classes have different functionality while sharing a common interface.” while sharing a common interface.”
  7. Polymorphism Interfaces interface MyInterface { public function doSomething(); } //

    All classes implementing the “MyInterface” interface must contain a public doSomething() method classA implements MyInterface classA implements MyInterface { public function doSomething() { } } class InvalidClass implements MyInterface { protected function doSomething() { } }
  8. Polymorphism Abstract Classes abstract class MyAbstractClass { abstract public function

    doSomething(); protected function _echo($a) { echo $a; } } // All classes extending the “MyAbstractClass” class must implement the doSomething() method classA extends MyAbstractClass { public function doSomething() { $this->_echo(“We did something.”); } }
  9. Magic Methods PHP supports several “magic” methods These methods are

    optional PHP will look for them in your classes at key moments moments We’ll be talking about just a couple: __construct __destruct __get __set
  10. Magic Methods __construct() This method will be called when an

    object is This method will be called when an object is first being instantiated. This is where you would commonly put all your initialization logic.
  11. Magic Methods __destruct() This is the counterpoint to the __construct

    This is the counterpoint to the __construct method. It is called when your object is destroyed by PHP.
  12. Magic Methods __destroy() class X extends DatabaseClass { public function

    __construct() { parent::_initDb(); parent::_initDb(); } public function __destroy() { parent::_closeDbConnection(); } }
  13. Magic Methods __get() This magic method allows you to “overload”

    a property. By using __get() you can have your object return data when a requested property doesn’t really exist. This is can be used to simulate read-only access to public attributes.
  14. Magic Methods __get() classA { protected $_vars = array(“foo” =>

    “bar”); public function __get($name) public function __get($name) { return $this->_vars[$name]; } } $MyA = new A; echo $MyA->foo; // echos: “bar” $MyA->foo = “foobar”; // Error, property “foo” is not defined
  15. Magic Methods __set() This is the counterpoint to __get() Using

    this method you can simulate setting an object’s public property that doesn’t really exist.
  16. Magic Methods __set() class A { protected $_vars = array(“foo”

    => “bar”); public function __get($name) { return $this->_vars[$name]; return $this->_vars[$name]; } public function __set($name, $value) { $this->_vars[$name] = $value; } } $MyA = new A; echo $MyA->foo; // echos: “bar” $MyA->foo = “foobar”; echo $MyA->foo; // echos: “foobar”
  17. Magic Methods Beware when implementing __get() and __set() methods. These

    methods should be used sparingly as they can make debugging a nightmare.
  18. Common Design Patterns Singleton “Singleton” classes are classes that can

    only be instantiated once. Examples might be a database handler or a session handling class.
  19. Common Design Patterns Singleton class SingletonClass { static protected $_instance;

    protected function __construct() { } public function getInstance() { if (self::$_instance instanceof SingletonClass){ if (self::$_instance instanceof SingletonClass){ return self::$_instance; } self::$_instance = new SingletonClass(); return self::$_instance; } } $Invalid = new SingletonClass(); // Error, cannot access the protected __construct method $Valid = SingletonClass::getInstance();
  20. Common Design Patterns Factory The “Factory” design pattern allows you

    to easily create new instances of related classes. Examples might be a factory for different types of system users.
  21. Common Design Patterns Factory classAnimal { static public function factory

    ($name) { $className = “Animal_” . $name; return new $className; } } } classAnimal_Cat extends Animal { } classAnimal_Dog extends Animal { } $MyDog = Animal::factory(“Dog”); $MyCat = Animal::factory(“Cat”);