1

Hi guys im getting an error when i try to use namespace like this. here is the first error whene running the code below.

Fatal error: Class B\C\ClassC' not found

// file: index.php

define("DS",DIRECTORY_SEPARATOR);
define("__PATH__",$_SERVER["DOCUMENT_ROOT"]);
include (__PATH__.DS.'B'.DS.'autoloader.php');
use A\ClassA;
$obj = new classA();

// file: B/autoloader.php

spl_autoload_register(function($class){
    require_once __PATH__.DS.$class.'.php';
});

// file: A/ClassA.php

namespace A;
use B\ClassB;
class ClassA extends ClassB{}

// file: B/ClassB.php

namespace B;
use B\C\ClassC;
class ClassB extends ClassC{}

// file: B/C/ClassC.php

namespace B\C;
class ClassC{}
2
  • I have tried the same code. I am not getting any error Commented Oct 9, 2014 at 14:53
  • TY for inspiration and a simplest autoloader function, here it just needed to str_replace() the \ to / and it was good to go! Commented Oct 23, 2019 at 2:09

1 Answer 1

1

Unless beginning with a \, all namespace references are relative to current namespace:

namespace Foo;
use Bar\Baz;    // refers to \Foo\Bar\Baz
use \Bar\Baz;   // refers to \Bar\Baz
Sign up to request clarification or add additional context in comments.

1 Comment

same problem Fatal error: Class B\C\ClassC' not found thank you for your response

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.