Tags

, , ,

 

object-oriented-design-patterns-in-php7 for web

object-oriented-design-patterns-in-php7 for web

PHP 7 has a full object model and now we consider it as fully object oriented programming (OOP) language.
For a small website, where four, five pages represent a simple profile, you don’t need OOP. Object Oriented Programming is targeted at larger applications. In addition, I hope, one day, you’ll definitely build a large web application with other team members. For that purpose and to master PHP, you need to learn and adapt to OOP style.
For that reason, OOP was created – to break up large applications when long function did not work.
In the beginning, programming started in a linear fashion. Programmers wrote code, line after line. It took time to debug. Then came the concept of functions. We started encapsulating our data into a function.

I’m a Full-Time writer. If you find these articles helpful please consider buying any of my books. You can buy from either Leanpub or Amazon or from my publisher Apress.

However, it had limitations. It broke up the code but the code would not run until called.
As time passed by, we had badly needed a change. Web applications started becoming larger in volume. The solution was to use objects. To start a car you need to start the engine, accelerate, use break and steering, and switch on AC, and so on. Lot of functions to be written. Lot of variables to be declared and assigned. How about wrap them all into a single conceptualized object – a car.
Now all the internal workings are hidden and as a car owner or a driver, you do not want to know how the internal workings play their roles. You know your car moves. It stops. It honks. You are happy.
4.1.1 Classes
When we describe someone as good, we actually refer to a “type”.
Classes are the type of objects. Like integer is a type of a variable. This conception is particularly important. A class is a type of an object and many things depend on that. The main purpose of object is to create a class first. Without a car class, you cannot create a car object.
If class is the type of an object, then the object is the instance of a class.
Let’s see an example first.


                
                // let us create a house building App
                // we need to create a type 
                // and we want to create different types of house objects
                
                class House {
                    
                    // each house has doors
                    // they could have different colors

                    public $door;

                    function __construct($door) {
                        
                        $this->door = $door;
                    }

                }
                
                $house1 = new House("Red");
                
                echo "We have just created a house with " . $house1->door . " door.";
                
                $house2 = new House("Blue");
                
                echo "We have just created a house with " . $house2->door . " door.";

Now we would like to see the similar output in the old procedural style. Consider this code:


                
                // let us assume we want to create the same functionality 
                // procedurally
                //$door = "RED";
                
                function House1($door) {
                    
                    //global $door;
                    $door = "Red";
                     echo "We have just created a house with " . $door . " door.";
                     
                }
                function House2($door) {
                    
                    //global $door;
                    $door = "Blue";
                     echo "We have just created a house with " . $door . " door.";
                     
                }
                
                House1("Red");
                House2("Blue");
                
                // for different houses we need to create different functions
                // no reusability
                // no separation of codes

The main problem is – for different houses, you need to create different functions. The whole process becomes cumbersome.
In the first scenario, we created one type of the house object. It was “class House”. In that type, we mentioned the “door” property so that each new house might have different colors. We did not have to write separate functions for each house. One class. One type. In addition, that was enough to create many houses.
Consider another example where in a “buying app” we set a minimum price. No one can buy a product paying less than 2 dollar.



                
                // imagine a buying app
               // where we set a price ranging between two values
                // 2 to 10 dollars
                
                class BuyMe {
                    
                    public $product;
                    
                    public $price;

                    function __construct($product) {
                        
                        $this->product = $product;
                    }
                    
                    public function setPrice($price) {
                        
                            $this->price = $price;
                            $minimumPrice = 2;
                            if($this->price price;}
                            
                    }
                    
                    public function getPrice() {
                        
                        return $this->price;
                        
                    }

                }
                
                $mobile = new BuyMe("Mobile");
                
                $mobile->setPrice(10);
                
                echo $mobile->getPrice();

We need to clarify a few things.
First, to create a class, we need a class keyword. Next, it is not mandatory that we should always write the __construct() method. Although, whenever an object is instantiated, this constructor method is called first. If you want to pass any data in the beginning, you can pass through it.
In the second example, we have used set() and get() methods. These setter and getter methods are extremely handy when we need some validation. Here the price is set so you get the price. If it were not, it would throw an exception.
Nothing is mandatory here. If you don’t want, just don’t write it.

Advertisements