title: php Call to a member function on null?
body: <p>I’m following this tutorial from laracast(<a href="https://laracasts.com/series/php-for-beginners" rel="nofollow noreferrer">https://laracasts.com/series/php-for-beginners</a>) and I’m at this episode(16 - Make a Router) in the series</p>
  
  <p>have done all sorts of things to understand one part in this tutorial but I could't after I spent many hours.</p>
  
  <p>The tutorial is about make routes system similar to laravel framework</p>
  
  <p>The route class</p>
  
  <p>
  
  <pre><code>/**
   * Created by PhpStorm.
   * User: ahmadz
   * Date: 7/2/2017
   * Time: 7:30 PM
   */
  class Router
  {
  
      public $route = [
          'GET' =&gt; [],
          'POST' =&gt; [],
          ];
  
      public static function load($file)
      {
            $router = new static;
  
            require $file;
  
            return $router;
      }
  
      public function get($name, $path)
      {
          $this-&gt;route['GET'][$name] = $path;
      }
  
      public function uriProcess($uri, $method)
      {
  
          if(array_key_exists($uri,$this-&gt;route[$method])){
              return   $this-&gt;route[$method][$uri];
          }
  
          throw  new Exception('Error in the uri');
      }
  }
  
  
      routes file
  $router-&gt;get('learn/try','controller/index.php');
  $router-&gt;get('learn/try/contact','controller/contact.php');
  </code></pre>
  
  <p>index file</p>
  
  <pre><code>require Router::load('routes.php')-&gt;uriProcess(Request::uri(), Request::method());
  </code></pre>
  
  <p>the problem occur when I change this to </p>
  
  <pre><code>  public static function load($file)
          {
  
                require $file;
  
          }
  </code></pre>
  
  <p>I removed these 2 lines  </p>
  
  <pre><code>  $router = new static;
  
     return $router;
  </code></pre>
  
  <p>and then instantiate an object in routes file </p>
  
  <pre><code>   $router = new Router;
  
      $router-&gt;get('learn/try','controller/index.php');
      $router-&gt;get('learn/try/contact','controller/contact.php');
  </code></pre>
  
  <p>When I do this I get these errors</p>
  
  <blockquote>
    <p>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</p>
  </blockquote>
  
  <p>Can you explain way I can't instantiate an object in  routes file instead of load function ?</p>