4

For instance, say I have the following code.

$foo = new bar();

And an autoloader like this.

function autoload($class_name) {
    $class_file_name = str_replace('_', '/', $class_name) . '.php';
    if (file_exists($class_file_name)) {
        include($class_file_name);
    }
}

But the class I really want to load is in the folder 'foo/bar.php', and the real class name is actually foo_bar. Is there a way to dynamically change the name of the class being autoloaded? For instance, something like this?

function autoload(&$class_name) {
    $class_name = 'foo_' . $class_name;
    $class_file_name = str_replace('_', '/', $class_name) . '.php';
    if (file_exists($class_file_name)) {
        include($class_file_name);
    }
}

I know if something like this is possible, it is not exactly best practice, but I would still like to know if it is.

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
dqhendricks
  • 19,030
  • 11
  • 50
  • 83

2 Answers2

4

No. You could load a different file. You could load no file. You could load several files. But after autoloading, PHP expects the class to exist like it was called.

If you call a class X, you can't magically give PHP class Y.

Maybe it's enough to set up the filesystem like that, but still keep literal class names?

PS
I've wanted this for a while too. When I didn't have access to namespaces yet. Now that I do, all my problems are solved =) If you do have access to namespaces, you should 'study' PSR-0.

Rudie
  • 52,220
  • 42
  • 131
  • 173
  • +1 for PSR-0. It's worth noting that PSR-0 doesn't require namespaces to operate, it simply works with them. There won't be a problem using the example code in PHP 5.2. – Charles May 02 '11 at 20:15
  • Fair enough. I never used it before namespaces, but I guess you're right. These days, I never have `_` in my class names anymore, so I guess you have a point. – Rudie May 02 '11 at 20:17
  • Yes, in the real code I am using a PSR-0 autoloader. This was just an example to simplify the question. The problem is that I do not want to have to prefix every class in the library folder with library_ or library\ for instance. – dqhendricks May 02 '11 at 21:11
  • Edit queue if full, here is new PSR-0 link: https://www.php-fig.org/psr/psr-0/ – Blizzardengle Mar 15 '22 at 20:55
1

Maybe you can use class_alias, this way:

  • PHP requires class X
  • You want load class Y instead.
  • You create an alias X for class Y, so X will behave like Y.

E.g.:

function autoload($class_name) {
    $real_class_name = 'foo_' . $class_name;
    $class_file_name = str_replace('_', '/', $real_class_name) . '.php';
    if (file_exists($class_file_name)) {
        include($class_file_name);
        class_alias($real_class_name, $class_name);
    }
}

Hope it can help you.

Sony Santos
  • 5,435
  • 30
  • 41
  • Pretty cool. Never knew this function existed. This may be what I am looking for. – dqhendricks May 02 '11 at 21:14
  • That's sick! (In a very kewl way!) But even if this enables you to do what you want: beware. See Matti Virkkunen's comment (it has 6 votes for a reason) – Rudie May 02 '11 at 21:41
  • The main drawback I see is: you must be aware that the two class names (real and alias) are loaded in your namespace; so try to avoid name conflicts. ;) – Sony Santos May 03 '11 at 11:33