Error including class in index file so I can’t instantiate it [PHP]

Asked

Viewed 27 times

-1

Hello I have two file o INDEX.PHP which is located at the root of the project and the Archive SYSTEM.PHP which is located inside the folder LIB.

Summary of the Archive INDEX

<?php 
  define('APP_ROOT', 'home');
  require_once 'config/Bootstrap.php';
  require_once 'lib/System.php';
  $System = new System();
  $System->start();

Archive summary SYSTEM

<?php 
  namespace lib;
  require_once 'Router.php';
  class System extends Router {

   Aqui contem o method **START**

  }

php returns the following error: Fatal error: Uncaught Error: Class 'System' not found in 'My directory'

I already gave a var_dump exists_file in the INDEX direct path to the lib/System.php path and returned TRUE, the path is correct but because I can’t find the class?

1 answer

1

Your class System belongs to the namespace lib and therefore its "full name" (or Fully-Qualified name) is lib\System. Therefore, to invoke it, you need to reference it by its "full name".

// ...
$System = new lib\System();

Or use aliases:

require_once 'caminho/para/arquivo.php';

use lib\System; 

$System = new System();

Or else,

require_once 'caminho/para/arquivo.php';

use lib\System as MyClass;

$System = new MyClass();
  • @Otávio If you think that my answer has solved your problem, click on the "vezinho" that is in my reply (see here how to accept an answer) to help the community. Thank you

Browser other questions tagged

You are not signed in. Login or sign up in order to post.