Blog2Works

This is the blog of freelance website designer and developer Ben Johnson, with thoughts on design, programming, frameworks, jquery and the latest technology.
Viewing entries tagged with 'OOPHP'
  • What is Object-Orientation?

    Author: Ben Johnson

    Tags: , , , ,

    PHP is one of the most popular scripting language on the planet A Little History of OOP in PHP OOP first introduced in PHP3, but with very basic feature PHP4 released, OO features get matured with huge performance improvement But, the PHP core team, rewrite the core engine, and released PHP5 with new completely new object model Procedural vs OO Coding Style <?php $user_input = $_POST[‘field‘]; $filtered_content = filter($user_input); //user input filtering mysql_connect("dbhost","dbuser","dbpassword'); //database mysql_select_db("dbname"); $sql = "some query";$result = mysql_query($sql); while ($data = mysql_fetch_assoc()) { process ($data); } process_user_input($filtered_content); ?> <?php $input_filter = new filter(); $input_filter->filter_user_input(); //filter the user inputs $db = new dal("mysql"); //data access layer $db->connect($dbconfig); //we wre using mysql $result = $db->execute($sql); ReportGenerator::makereport($result); //process data $model = new Postmodel($filter->get_filtered_content()); $model->insert(); ?> Benefits of OOP Reusability Refactoring Extensible Maintenance Efficiency Dissection of an Object <?php //class.emailer.php class emailer { private $sender; private $recipients; private $subject; private $body; function __construct($sender) { $this->sender = $sender; $this->recipients = array(); } public function addRecipients($recipient) { array_push($this->recipients, $recipient); } public function setSubject($subject) { $this->subject = $subject; } public function setBody($body) { $this->body = $body; } public function sendEmail() { foreach ($this->recipients as $recipient) { $result = mail( $recipient, $this->subject, $this->body, "From: {$this->sender} " ); if ($result) { echo "Mail successfully sent to {$recipient}<br/>"; } } } } ?> Difference of OOP in PHP4 and PHP5 In PHP4 everything is open, you can't use public, private or protected In PHP4 you can find interfaces but no abstract or final keyword In PHP4 there are no multiple inheritances for interfaces In PHP4, almost everything is static There is no class-based constant in PHP4. There is no static property in objects in PHP4, and there is no destructor in PHP4 objects. Whenever an object is copied, it is a shallow copy of that object. But in PHP5 shallow copy is possible only using the clone keyword. There is no exception object in PHP4. But in PHP5 exception management is a great added feature. There were some functions to investigate methods and properties of a class in PHP4, but in PHP5 beside those functions, a powerful set of API (Reflection API) is introduced for this purpose. Method overloading via magic methods like __get() and __set() are available in PHP5. There are also lots of built-in objects to make your life easier. But most of all, there is a huge performance improvement in PHP5 for OOP. Some Basic OO Terms Class A class is a template for an object. A class contains the code which defines how an object will behave and interact either with each other, or with it. Every time you create an object in PHP, you are actually developing the class. Property A property is a container inside the class which can retain some information. Unlike other languages, PHP doesn't check the type of property variable. A property could be accessible only in class itself, by its subclass, or by everyone. In essence, a property is a variable which is declared inside the class itself, but not inside any function in that class. Method Methods are functions inside a class. Like properties, methods can also be accessible by those three types of users. Encapsulation Encapsulation is the mechanism that binds together code and the data it manipulates, and keeps both safe from outside interference and misuse. The wrapping up of data and methods into a single unit (called class) is known as encapsulation. The benefit of encapsulating is that it performs the task inside without making you worry. Polymorphism Objects could be of any type. A discrete object can have discrete properties and methods which work separately to other objects. However a set of objects could be derived from a parent object and retain some properties of the parent class. This process is called polymorphism. An object could be morphed into several other objects retaining some of its behaviour. Inheritance The key process of deriving a new object by extending another object is called inheritance. When you inherit an object from another object, the subclass (which inherits) derives all the properties and methods of the superclass (which is inherited). A subclass can then process each method of superclass anyway (which is called overloading) Coupling Coupling is the behaviour of how classes are dependent on each other. Loosely coupled architecture is much more reusable than tightly coupled objects. In the next chapter we will learn details about coupling. Coupling is a very important concern for designing better objects. Design Patterns First invented by the &quot;Gang of Four&quot;, design patterns are just tricks in object oriented programming to solve similar sets of problems with a smarter approach. Using design patterns (DP) can increase the performance of your whole application with minimal code written by developers. Sometimes it is not possible to design optimized solutions without using DP. But unnecessary and unplanned use of DP can also degrade the performance of your application Subclass A very common term in OOP, and we use this term throughout this book. When an object is derived from another object, the derived one is called the subclass of which it is derived from. Superclass A class is superclass to an object if that object is derived from it. To keep it simple, when you extend an object, the object which you are extending is the superclass of a newly extended object. Instance Whenever you create an object by calling its constructor, it will be called an instance. To simplify this, whenever you write some thing like this $var = new Object(); you actually create an instance of object class.