Tags

, , , , , ,

php7 copy//embedr.flickr.com/assets/client-code.js
Your framework should be specific. It should not call a lot of other functions and drag them along with single request making your application slow.
The first step could be a simple Router.
This Router object will show you how you can pass a URI and find the desired page.
Let’s go and make it.


$router = [
                    
                    "/" => "index.php",
                    "/about" => "about.php"
                    
                ];
                
                class Router {
                    
                    public $array = [];
                    public $url;
                    public $destination;

                    function __construct($array) {
                        $this->array = $array;
                    }
                    
                    function redirect($url, $destination) {
                        $this->url = $url;
                        $this->destination = $destination;
                        
                        if(array_key_exists($this->url, $this->array)){
                            
                            return $this->array["{$this->url}"];
                            
                        }
                        else{
                            
                            return FALSE;
                            
                        }
                    }

                }
                
                $route = new Router($router);
                
                echo $route->redirect("/", "index.php");




Don’t forget to add the PHP tag.

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

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


We can try the same thing in a different way using PHP overloading methods.


class OverloadingClass {
                    
                    protected $uri;
                    protected $response;

                    function __construct() {
                        
                        echo 'This is overloading constructor.
'; } function getResponse() { return "Here is the URI = " . $this->uri . " and here is the Response = " . $this->response . "
"; } function setResponse($response) { $this->response = $response; } //we are overloading the uri and response at the same time function __call($name, $arguments) { if($name == "setURI"){ if(count($arguments) == 1){ $this->uri = $arguments[0]; } if(count($arguments) == 2){ $this->uri = $arguments[0]; $this->response = $arguments[1]; } } } } echo 'CReating our new router...
'; $router = new OverloadingClass(); $router->setURI("/about"); $router->setResponse("about.php"); echo $router->getResponse(); $router->setURI("/contact", "contact.php"); echo $router->getResponse();

The output is nteresting.

CReating our new router…
This is overloading constructor.
Here is the URI = /about and here is the Response = about.php
Here is the URI = /contact and here is the Response = contact.php

Now the possibility is endless.

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.

Advertisements