0

I found some code of router that neednt use require or useto create object

<?php
/**
 * Front controller
 *
 * PHP version 7.0
 */
/**
 * Composer
 */
require dirname(__DIR__) . '/vendor/autoload.php';
/**
 * Error and Exception handling
 */
error_reporting(E_ALL);
set_error_handler('Core\Error::errorHandler');
set_exception_handler('Core\Error::exceptionHandler');
/**
 * Routing
 */
$router = new Core\Router();
// Add the routes
$router->add('', ['controller' => 'Home', 'action' => 'index']);
$router->add('{controller}/{action}');

$router->dispatch($_SERVER['QUERY_STRING']);

and

// Require composer autoloader
require __DIR__ . '/vendor/autoload.php';

// Create Router instance
$router = new \Bramus\Router\Router();

// Define routes
// ...

// Run it!
$router->run();

or i missed something or we can actually create object withou require class file ?. This is my code, but it wouldnt work

require_once dirname(__DIR__) . '/vendor/autoload.php';

  //require_once "../app/Config.php";

  //require_once '../core/Router.php';

  //use core\Router;

  $router = new \app\core\Router();
  • You don't need to use `required/include` if the definition of the class is in the same file of your script. You don't need `use` if you provide the full namespace of the class – catcon Aug 09 '19 at 06:13
  • The autoloader is taking care of loading the necessary files for these classes (if they exist and they are in the appropriate directory). Read more on [autoloading classes](https://www.php.net/manual/en/language.oop5.autoload.php). – yivi Aug 09 '19 at 06:32

0 Answers0