How do I autoload a class with the X Autoload module? I have X Autoload installed and enabled. I'm trying to get a simple example to work. The below setup throws the error:


  Fatal error: Class 'Pet' not found in
  MY-SITE/sites/all/modules/foo/foo.module on line 15


Have I got the directory structure correct for pet.php? I'm finding the docs difficult to follow on the placement and naming of the file that contains the class definitions. I'm trying to follow the PSR-0 standard to begin with. Thanks.

UPDATE: The below has now been corrected as per Clive's answer.

foo.info

name = Foo
core = 7.x
dependencies[] = xautoload (&gt;= 7.x-4.0)


foo.module

&lt;?php

function foo_menu() {
  return array(
    'foo' =&gt; array(
      'title' =&gt; 'Foo',
      'page callback' =&gt; 'foo_page',
      'access callback' =&gt; 'user_access',
      'access arguments' =&gt; array('access content'),
    ),
  );
}

function foo_page() {
  $output = \Drupal\foo\Pet::dog();
  return array(
    '#type' =&gt; 'markup',
    '#markup' =&gt; $output,
  );
}


Pet.php (MY-SITE/sites/all/modules/foo/lib/Drupal/foo/Pet.php)

&lt;?php

namespace Drupal\foo;

class Pet {
  public static function dog() {
    return 'Buster';
  }
}
If your file-system is case-sensitive, so is PSR-0. Try changing your class filename to


  MY-SITE/sites/all/modules/foo/lib/Drupal/foo/Pet.php


And make sure you're namespacing your class correctly, e.g.

namespace Drupal\foo;

class Pet {
  ...
}


Lastly, make sure you're referencing the class correctly in your module file. Either

// Top of file.
use Drupal\foo\Pet;

// In a hook or whatever
$dog = Pet::dog();

or

// In a hook
$dog = \Drupal\foo\Pet::dog();