Trying to access array offset on value of type null in /opt/lampp/htdocs/oopsandbox/traversymvc/app/libraries/Core.php on line 18

I start learning php oop mvc and I have following the tutorial step by step and having this notice: Trying to access array offset on value of type null in /opt/lampp/htdocs/oopsandbox/traversymvc/app/libraries/Core.php on line 18. I only have this notice on my Home page.

My code is: Core.php :

 <?php
      /*
       * App Core Class
       * Creates URL & loads core controller
       * URL FORMAT - /controller/method/params
       */
      class Core {
        protected $currentController = 'Pages';
        protected $currentMethod = 'index';
        protected $params = [];
    
        public function __construct(){
          //print_r($this->getUrl());
    
          $url = $this->getUrl();
    
          // Look in controllers for first value
          if(file_exists('../app/controllers/' . ucwords($url[0]). '.php')){
            // If exists, set as controller
            $this->currentController = ucwords($url[0]);
            // Unset 0 Index
            unset($url[0]);
            print $url[0];
    
          }
    
          // Require the controller
          require_once '../app/controllers/'. $this->currentController . '.php';
    
          // Instantiate controller class
          $this->currentController = new $this->currentController;
    
          // Check for second part of url
          if(isset($url[1])){
            // Check to see if method exists in controller
            if(method_exists($this->currentController, $url[1])){
              $this->currentMethod = $url[1];
              // Unset 1 index
              unset($url[1]);
            }
          }
    
          // Get params
          $this->params = $url ? array_values($url) : [];
    
          // Call a callback with array of params
          call_user_func_array([$this->currentController, $this->currentMethod], $this->params);
        }
    
        public function getUrl(){
          if(isset($_GET['url'])){
            $url = rtrim($_GET['url'], '/');
            $url = filter_var($url, FILTER_SANITIZE_URL);
            $url = explode('/', $url);
            return $url;
          }
        }
      } 
***

congig.php :

***
<?php
  // DB Params
  define('DB_HOST', 'localhost');
  define('DB_USER', 'root');
  define('DB_PASS', '');
  define('DB_NAME', 'eShop');

  // App Root
  define('APPROOT', dirname(dirname(__FILE__)));
  // URL Root
  define('URLROOT', '_YOUR_URL_');
  // Site Name
  define('SITENAME', '_YOUR_SITENAME_');
***

Controller.php

***<?php
  /*
   * Base Controller
   * Loads the models and views
   */
  class Controller {
    // Load model
    public function model($model){
      // Require model file
      require_once '../app/models/' . $model . '.php';

      // Instatiate model
      return new $model();
    }

    // Load view
    public function view($view, $data = []){
      // Check for view file
      if(file_exists('../app/views/' . $view . '.php')){
        require_once '../app/views/' . $view . '.php';
      } else {
        // View does not exist
        die('View does not exist');
      }
    }
  }***

Database.php

***<?php
  /*
   * PDO Database Class
   * Connect to database
   * Create prepared statements
   * Bind values
   * Return rows and results
   */
  class Database {
    private $host = DB_HOST;
    private $user = DB_USER;
    private $pass = DB_PASS;
    private $dbname = DB_NAME;

    private $dbh;
    private $stmt;
    private $error;

    public function __construct(){
      // Set DSN
      $dsn = 'mysql:host=' . $this->host . ';dbname=' . $this->dbname;
      $options = array(
        PDO::ATTR_PERSISTENT => true,
        PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
      );

      // Create PDO instance
      try{
        $this->dbh = new PDO($dsn, $this->user, $this->pass, $options);
      } catch(PDOException $e){
        $this->error = $e->getMessage();
        echo $this->error;
      }
    }

    // Prepare statement with query
    public function query($sql){
      $this->stmt = $this->dbh->prepare($sql);
    }

    // Bind values
    public function bind($param, $value, $type = null){
      if(is_null($type)){
        switch(true){
          case is_int($value):
            $type = PDO::PARAM_INT;
            break;
          case is_bool($value):
            $type = PDO::PARAM_BOOL;
            break;
          case is_null($value):
            $type = PDO::PARAM_NULL;
            break;
          default:
            $type = PDO::PARAM_STR;
        }
      }

      $this->stmt->bindValue($param, $value, $type);
    }

    // Execute the prepared statement
    public function execute(){
      return $this->stmt->execute();
    }

    // Get result set as array of objects
    public function resultSet(){
      $this->execute();
      return $this->stmt->fetchAll(PDO::FETCH_OBJ);
    }

    // Get single record as object
    public function single(){
      $this->execute();
      return $this->stmt->fetch(PDO::FETCH_OBJ);
    }

    // Get row count
    public function rowCount(){
      return $this->stmt->rowCount();
    }
  }***

How can i fix this error.

          $url = $this->getUrl();
    
          // Look in controllers for first value
          if(file_exists('../app/controllers/' . ucwords($url[0]). '.php')){

$this->getUrl() is returning null, which you’re then trying to access like an array. getUrl can return an array or null, so you need to check for both values.

If that error message is confusing you, you might want to take a step back and look at the fundamentals of the language some more.

Sponsor our Newsletter | Privacy Policy | Terms of Service