Ignore folder and classes with __autoload

Asked

Viewed 59 times

1

I have the following folder structure in my project:

inserir a descrição da imagem aqui

Within app I have the following structure:

inserir a descrição da imagem aqui

And the following native function of the php to give auto load in the classes:

<?php

require_once '/app/config.php';

function __autoload($class_name) {

    $folders = array(
        'class',
    );

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

        var_dump($path);

        if (file_exists($path)) {
            require_once $path;
        } else {
            die('File not found: ' . $path);
        }
    }
}

I would like to know how to ignore the folder twitteroauth and the classes within it. It will be possible?

UPDATE

I tried with spl_autoload_register(); and get the same mistake:

File not found in: C: wamp64 www twitter-login app class Oauthexception.php

<?php
function autoload ($class_name) {
    $folders = array(
        'class',
    );

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

        if (file_exists($path)) {
            require_once $path;
        } else {
            die('File not found in: ' . $path);
        }
    }
}

spl_autoload_register("autoload");

1 answer

1

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

Note: Will show errors if class does not exist, test, enter in 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;
      }
        }
    }
});
  • 1

    Its very good, vlw men, worked aoksokaosk.

  • 1

    Arrange it. Buddy

Browser other questions tagged

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