1

I just wanna use a namespace in another file, to use the class in it, but im too retarded to do this...

first file:

namespace fun;
use fun\kjr\trouble;

$trouble = new trouble('http://someURL');

second file:

namespace fun\kjr;

class trouble { ... }

This is the error i get:

Error: Fatal error: Class 'fun\kjr\trouble' not found in D:\wamp\www\fun\index.php on line 8

Where did I declare a wrong namespace? Oo

Greetings

14
  • Your second file doesn't contan valid code. Commented Apr 10, 2014 at 17:39
  • Neither is there any reference to ReadRSS Commented Apr 10, 2014 at 17:39
  • Sry, there's alot more in it, but I think only that part is needed to find the error^^ sorry Commented Apr 10, 2014 at 17:40
  • 6
    Are you using some kind of autoloading? If not, did you include the file? Autoloading isn't "auto" unless an appropriate loader is defined. Commented Apr 10, 2014 at 17:40
  • 1
    @Xatenev Nope. The use makes the class available in the current namespace without prefixing its own namespace, but does not include/require the file. You have to do that yourself (or let your autoloader do it) Commented Apr 10, 2014 at 17:42

2 Answers 2

1

I had to include my class aswell.

use namespace

only makes the class available in the actual context.

It does NOT include it, so you can cause it.

use

include_once('your.file')

to do that!

Thanks @Michael Berkowski for helping.

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

Comments

0

The problem (from what I can see) is you haven't specified an autoloader (e.g. with spl_autoload_register()) or haven't included the file in your code. The use statement will only find a class for you if you have an autoloader in place, otherwise you must include your code ahead of time with include/include_once or require/require once.

I would start with making an autoloader and registering it and then it may be wise to map your namespaces to directory structure as specified by PSR-0.

Comments

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.