Search code examples
phpclassurl-routing

php Call to a member function on null?


I’m following this tutorial from laracast(https://laracasts.com/series/php-for-beginners) and I’m at this episode(16 - Make a Router) in the series

have done all sorts of things to understand one part in this tutorial but I could't after I spent many hours.

The tutorial is about make routes system similar to laravel framework

The route class

/**
 * Created by PhpStorm.
 * User: ahmadz
 * Date: 7/2/2017
 * Time: 7:30 PM
 */
class Router
{

    public $route = [
        'GET' => [],
        'POST' => [],
        ];

    public static function load($file)
    {
          $router = new static;

          require $file;

          return $router;
    }

    public function get($name, $path)
    {
        $this->route['GET'][$name] = $path;
    }

    public function uriProcess($uri, $method)
    {

        if(array_key_exists($uri,$this->route[$method])){
            return   $this->route[$method][$uri];
        }

        throw  new Exception('Error in the uri');
    }
}


    routes file
$router->get('learn/try','controller/index.php');
$router->get('learn/try/contact','controller/contact.php');

index file

require Router::load('routes.php')->uriProcess(Request::uri(), Request::method());

the problem occur when I change this to

  public static function load($file)
        {

              require $file;

        }

I removed these 2 lines

  $router = new static;

   return $router;

and then instantiate an object in routes file

   $router = new Router;

    $router->get('learn/try','controller/index.php');
    $router->get('learn/try/contact','controller/contact.php');

When I do this I get these errors

Fatal error: Uncaught Error: Call to a member function uriProcess() on null in C:\xampp\htdocs\learn\try\index.php on line 12 ( ! ) Error: Call to a member function uriProcess() on null in C:\xampp\htdocs\learn\try\index.php on line 12

Can you explain way I can't instantiate an object in routes file instead of load function ?


Solution

  • You've removed the important part of your code.

    In your load() method you actually instantiate the Router class and then return the newly created $router object.

    When you remove the following lines:

       $router = new static;
    
       return $router;
    

    The method load() returns nothing, hence you get the aforementioned error.

    You've to understand, you're trying to use the method uriProcess() which is a method of the class Router, but how do you expect this method to work when you don't have any object in your hand?

    You will have to use the code you've shown at the beginning:

    public static function load($file)
    {
        $router = new static;
    
        require $file;
    
        return $router;
    }
    

    Edit:

    After I understood what you meant, you may try the following code:

    Router::load('routes.php');
    
    $router = new Router;
    $router->uriProcess(Request::uri(), Request::method());
    
    $router->get('learn/try', 'controller/index.php');
    $router->get('learn/try/contact', 'controller/contact.php');