Ignore folder and classes with __autoload

1

I have the following folder structure in my project:

WithinappIhavethefollowingstructure:

Andthefollowingnativefunctionofphptogiveautoloadinclasses:

<?phprequire_once'/app/config.php';function__autoload($class_name){$folders=array('class',);foreach($foldersas$folders_files){$path=ROOT.'app'.SEPARATOR.$folders_files.SEPARATOR.$class_name.'.php';var_dump($path);if(file_exists($path)){require_once$path;}else{die('Filenotfound:'.$path);}}}

I'dliketoknowhowtoignorethetwitteroauthfolderandtheclasseswithinit.Isitpossible?

UPDATE

Itriedwithspl_autoload_register();andgotthesameerror:

  

Filenotfoundin:C:\wamp64\www\twitter-login\app\class\OAuthException.php

<?phpfunctionautoload($class_name){$folders=array('class',);foreach($foldersas$folders_files){$path=ROOT.'app'.SEPARATOR.$folders_files.SEPARATOR.$class_name.'.php';if(file_exists($path)){require_once$path;}else{die('Filenotfoundin:'.$path);}}}spl_autoload_register("autoload");
    
asked by anonymous 24.06.2017 / 16:21

1 answer

2

Try this way, without using else with die so it will ignore and will not show errors. Only exceptions of own wampserver .

Note: It will show errors if the class does not exist, test it, insert it into your code, $teste = new Teste; .

<?php
spl_autoload_register(function ($class) {

    $folders = array(
        'class'
    );

    foreach ($folders as $folders_files) {
        $path = ROOT . 'app' . SEPARATOR . $folders_files . SEPARATOR . $class . '.php';


        if (preg_match('/[a-zA-Z]+$/', $class)) {
      if (file_exists($path)) {
          require $path;
          return true;
      }
        }
    }
});
    
24.06.2017 / 17:03