technosophos / querypath

QueryPath is a PHP library for manipulating XML and HTML. It is designed to work not only with local files, but also with web services and database resources.
http://querypath.org
Other
822 stars 114 forks source link

Class '\\QueryPath\\DOMQuery' not found in QueryPath/QueryPath.php #164

Closed turboflash closed 9 years ago

turboflash commented 9 years ago

I hit into the subject error and I did some quick investigation on why it occurs. It seems the issue happens in the autoload code below.

    spl_autoload_register(function ($klass) {
      $parts = explode('\\', $klass);
      file_put_contents('/tmp/output.log', var_export(array($klass, $parts), true), FILE_APPEND);
      if ($parts[0] == 'QueryPath') {
        $path = __DIR__ . '/' . implode('/', $parts) . '.php';
        if (file_exists($path)) {
          require $path;
        }
      }
    });

$klass was assigned '\QueryPath\DOMQuery' when the function is called. After exploding $klass into $parts, the following is the resulting array. require $path would not be executed as $parts[0] is not equal to 'QueryPath'.

  array (
    0 => '',
    1 => 'QueryPath',
    2 => 'DOMQuery',
  )
technosophos commented 9 years ago

So I'm thinking just doing something like this:

if ($parts[0] == '') {
  array_shift($parts)
}

That would fix the use case, and hopefully stay backward compatible. See any issues with that?

turboflash commented 9 years ago

Nope. That resolves the issue. ;)