0

I'm new to namespaces in PHP and trying to make use of them to load classes.

Whenever I run my code I get class Cheese cannot be found on line x

PHPstorm recognizes the class via the namespaces and enables its methods.

I have the following files / directory structure.

/Project 
  /App
     Mouse.php
  /Test
     MouseTest.php

Mouse.php

namespace App\Mouse;

class Cheese 
{
}

MouseTest.php

namespace Test\MouseTest;

use \App\Mouse\Cheese as Cheese;

class CheeseTest 
{
   function test() {
        $cheese = new Cheese();
        $cheese->eat();
   } 

}
1
  • 3
    What kind of autoloader are you using? Commented Apr 20, 2015 at 18:49

3 Answers 3

1

if you use composer or any autoloader that follow psr-0, then file name must same with class name, change Mouse.php to Cheese.php

Sign up to request clarification or add additional context in comments.

1 Comment

I just noticed the Class was a different name than the file. I missed that, so yes, not quite PSR0 like I had thought.
0

This looks like a similar structure to the PSR0 standard found here: http://www.php-fig.org/psr/psr-0/

This is an example autoloader following that structure:

function autoload($className)
{
    $className = ltrim($className, '\\');
    $fileName  = '';
    $namespace = '';
    if ($lastNsPos = strrpos($className, '\\')) {
        $namespace = substr($className, 0, $lastNsPos);
        $className = substr($className, $lastNsPos + 1);
        $fileName  = str_replace('\\', DIRECTORY_SEPARATOR, $namespace) . DIRECTORY_SEPARATOR;
    }
    $fileName .= str_replace('_', DIRECTORY_SEPARATOR, $className) . '.php';

    require $fileName;
}
spl_autoload_register('autoload');

The spl_autoload_register call registers the autoload function so when a class is instantiated, it will use your autoload function to retrieve the class definition.

2 Comments

how would i use this. would i have to predict classes to autoload
@user3708645, you don't have to predict classes. The autoload function is invoked with the className. From the class name, the PSR0 function discerns the location of the file to include. For example: App\Mouse\Cheese would look in App/Mouse/Cheese.php for the Cheese class.
0

PHP namespaces don't load classes automatically, you have to include their files.

To make it automatic (like you want) you have to make an autoloader which will load the classes depending on the namespaces that you want to use.

The best way to make this, is using composer: http://code.tutsplus.com/tutorials/easy-package-management-with-composer--net-25530

but I recommend to search "php auloader" in google

1 Comment

Seems to have a certificate error on that site. It might be better for you to include more details yourself, especially if that site doesn't exist in the future.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.