Problem during the self-loading of classes and traits in PHP

0

Good afternoon,

I'm working on an API using PHP as the programming language for it.

What is the problem?

In my autoloader , I include each file with extension .php in the same directory that means a class each (with a foreach() , using require() ) , but when it includes classes for example CreateUrl that use traits, it returns the following error: Fatal error: Trait 'Endpoint' not found in /home/web/site.com/api/v2/classes/CreateUrl.php on line 11 and autoloading stops . What I do not understand is, I have not initialized as the class says and still the autoloader does not finish loading the classes, so I do not understand.

Thanks for your answers.

What have I tried? This has been the attempt (to use traits ) to be able to extend / use methods and properties of more than one class.

Autoload.php file:

<?php

const AUTOLOAD_DIRECTORY = '/home/web/site.com/classes/';

foreach(glob(AUTOLOAD_DIRECTORY.'*.php') as $class){
  if(preg_match('/autoload.php/', $class)){
    continue;
  }
  require $class;
}

foreach(glob(AUTOLOAD_DIRECTORY.'*.php') as $class){
  if(preg_match('/autoload.php/', $class)){
    continue;
  }
  $class = str_replace('.php', '', $class);
  new $class;
}
    
asked by Derek Nichols 14.08.2017 в 17:54
source

0 answers