Classes define how their instances (objects) will instances (objects) will operate. Objects can contain “private” information and methods. Classes can inherit from other classes
public $bVariable; private $cVariable; private $cVariable; protected function __construct() { } private function __destruct() { } public function doSomething() { } }
public $bVariable; private $cVariable; protected function __construct() { } private function __destruct() { } public function doSomething() { } } class B extends A { public function doSomething() { parent::doSomething(); } }
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.”
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() { } }
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.”); } }
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.
“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