How to apply a custom namespace to an external library without changing its source?

Is it possible to use an external PHP library with custom namespace, without changing its source?

I want to use all external libraries with namespace prefix "ext".

For example, I want to be able to instantiate Predis 's Client class as new ext\Predis\Client() instead of new Predis\Client() (from root).

ps.: I'm autoloading all internal classes like this:

spl_autoload_register(function($class)
{
  $path = str_replace("\\", "/", $class);

  $file = __DIR__ . "/" . $path . ".php";

  require_once $file;
});

Upvotes: 1

Views: 350

Answers (1)

camelsWriteInCamelCase
camelsWriteInCamelCase

Reputation: 1665

It's not possible. Namespaces are resolved at compile time. It also contradicts the PSR standards:

The fully qualified class name MUST have a top-level namespace name, also known as a “vendor namespace”.

Upvotes: 1

Related Questions