0

on index.php I have below code

require 'Bootstrap.php';
require 'Variables.php';

function __autoload($class){
    $class = str_replace('Control\\', '', $class);
    require_once 'class/'.$class.'.php';
}

$bootstratp = new Control\Bootstrap();

on Bootstrap.php

namespace Control;
class Bootstrap{
  function __construct(){
    Constructor::load_html();
    self::same_namespace_different_class();
  }
  static function same_namespace_different_class(){
    Variables::get_values();
  }
}

in class/Constructor.php

class Constructor{
  static function load_html(){
    echo 'html_loaded';
  }
  static function load_variables(){
     echo 'load variables';
  }
}

and on Variables.php

namespace Control;
class Variables{
    static function get_values(){
        Constructor::load_variables();
    }
}

Assume, In total I have 4 PHP files including 3 Class files of 2 different namespaces. I also have a __autoload function on index.php that will call classes from 'class' folder but my 'Control' namespace files are in root folder.

When I echo the class name in __autoload i get the all the class names starting with 'Control\' even when I am calling a class from global namespace.

I am getting below error

Warning: require_once(class/Variables.php): failed to open stream: No such file or directory in _____________ on line 10

what is wrong with my code??

Siguza
  • 21,155
  • 6
  • 52
  • 89
Yellow and Red
  • 695
  • 9
  • 31

1 Answers1

1

When I echo the class name in __autoload i get the all the class names starting with 'Control\' even when I am calling a class from global namespace.

This is because in Bootstrap.php all the code is in Control namespace (namespace Control). So when you use:

Variables::get_values();

you call

\Control\Variables::get_values();

if you want to use Variables from global namespace you should use here:

\Variables::get_values();

Of course, the same happens for in Variables.php file:

Constructor::load_variables();

As Constructor is defined in global namespace (in class/Constructor.php there is no namespace used), you should access it here using:

\Constructor::load_variables();

If it's still unclear you could also look at this question about namespaces in PHP.

You should also notice that using __autoload is not recommended. You should use spl_autoload_register() now. Documentation about autoloading

Community
  • 1
  • 1
Marcin Nabiałek
  • 109,655
  • 42
  • 258
  • 291
  • Marcin, Thanks a lot, That was great and fast, – Yellow and Red Aug 07 '14 at 07:23
  • Is there a way to call the global namespace without adding line `$class = str_replace('Control\\', '', $class);` in autoloader function. – Yellow and Red Aug 07 '14 at 07:24
  • 1
    @yellowandred In fact you should leave your autoload function unchanged - only `require_once 'class/'.$class.'.php';` should be there and in other classes you should use `\` at the beginning before class name if you want to use class from global namespace as I showed you in answer. You shouldn't remove namespace from class if class is in global namespace in autoloader because if you have classes in different namespaces it will cause serious problem. It would be also bad practice to do that. – Marcin Nabiałek Aug 07 '14 at 07:40