luni, 22 noiembrie 2010

Understanding PHP’s OOP – basic terms explained

Hello there! I’ll explain some basic terms of PHP’s Object Orientated Programming terms that any developer needs to know. Perhaps you already use all of those, but if you don’t know which is which, you will have a problem in team communication. If so, please read on.


First of all, to understand any PHP OOP concept, you need to know the PHP basics. Then I need you to read and understand next paragraph – Procedural vs OO Programming:


Procedural programming creates a step by step program that guides the application through a sequence of instructions. Each instruction is executed in order. Procedural programming also focuses on the idea that all algorithms are executed with functions and data that the programmer has access to and is able to change. Object-Oriented programming is much more similar to the way the real world works; it is analogous to the human brain. Each program is made up of many entities called objects. Objects become the fundamental units and have behavior, or a specific purpose, associated with them. Objects cannot directly access another object’s data. Instead, a message must be sent requesting the data, just like people must ask one another for information; we cannot see inside each other’s heads.


Class


As Wikipedia says “A class is a template for an object, a user-defined datatype that contains variables, properties, and methods. You should imagine any class as a wrapper of functions (methods) that will help you manipulate some specific data. Well, I’ll give you an example:

//example filename would be simpleclass.php< ?phpclass SimpleClass{// property declarationpublic $var = 'a default value';// method declarationpublic function displayVar() {echo $this->var;}}?>

Here you can see syntax used in declaration of classes and an example method inside it, together with declaration of property.


Method


A method is just an expression for a function bound to a class. You can see an example of method (displayVar method) in declaration of class SimpleClass above.


Instance


Instance is an object (datatype) created by your class. The object consists of state and the behavior that’s defined in the object’s classes. So, take a look at this:

//example filename would be create_instance.php< ?phpinclude "simpleclass.php";$ourObject = new SimpleClass();$ourObject->displayVar();?>

In this example “$ourObject = new SimpleClass();” is line of code that created and instance (object) of SimpleClass class. And next line “$ourObject->displayVar();” accesses method of SimpleClass class on $ourObject instance.


Inheritance


Inheritance is a process in which a class inherits all the state and behavior of another class. in PHP, this is achieved when you extend existing class with your own. Take a look:

//example filename would be simpleclass.php< ?phpinclude "SimpleClass.php";class MyClass extends SimpleClass{// class property declaration of "name"public $name = null;public function assignValueToObjectVar($val) {$this->name = $val;}public function printValueFromObjectVar() {echo $this->name;}}//here we'll create a instance of MyClass:$myObject = new MyClass();//we assigned value "Test Name" to object's $name property$myobject->assignValueToObjectVar('Test Name');//we called method that echoes value from object's $name property$myObject->printValueFromObjectVar();?>

Last part of example is self-explanatory, I hope. 


Abstraction


Abstraction refers to the act of representing essential features without including the background details or explanations. It reduces and factors out details so that one can focus on a few concepts at a time. Take a look at php.net‘s example:

< ?phpabstract class AbstractClass{// Force Extending class to define this methodabstract protected function getValue();abstract protected function prefixValue($prefix);// Common methodpublic function printOut() {print $this->getValue() . "\n";}}class ConcreteClass1 extends AbstractClass{protected function getValue() {return "ConcreteClass1";}public function prefixValue($prefix) {return "{$prefix}ConcreteClass1";}}class ConcreteClass2 extends AbstractClass{public function getValue() {return "ConcreteClass2";}public function prefixValue($prefix) {return "{$prefix}ConcreteClass2";}}$class1 = new ConcreteClass1;$class1->printOut();echo $class1->prefixValue('FOO_') ."\n";$class2 = new ConcreteClass2;$class2->printOut();echo $class2->prefixValue('FOO_') ."\n";?>

In this example you can see “abstract level” in declaration of AbstractClass which is then extended by ConcreteClass1 and ConcreteClass2. All of them have same methods but you can add another method to AbstractClass that will do a bit more complex work in both ConcreteClass1 and ConcreteClass2, without the need to write that “universal logic” to both of extend classes.


Singleton


Singleton restricts the instantiation of a class to one object. You can look at the class itself as an instance (object). To give you an example where a singleton is widely used – with database connection. There’s no need to create a new database connection on each class you use in a single moment, but rather, you can use one connection through the project:

class Database{// Store the single instance of Databaseprivate static $instance;private function __construct() {//database connection goes hereself::$instance = $link;}public static function getInstance(){if (!self::$instance){self::$instance = new Database();}return self::$instance;}//our method that does something with databasepublic static function myDbMethod(){...}}//Usage of singleton classes differ a bit from classic approach//where you create n instance and then do something with it.$DB = Database::getInstance();$Result = $DB->myDbMethod();

I hope this will help with the understanding of basics needed for any actual object orientated programming.


View the original article here


eBooks on the French Canals, Living Aboard Around the World, La Rochelle Guide Book


Check it out!

Niciun comentariu:

Trimiteți un comentariu