在PHP中调用不同的命名空间或类

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??

查看全部
doukezi4576
doukezi4576
2014/08/07 06:29
  • autoload
  • php
  • class
  • namespaces
  • 点赞
  • 收藏
  • 回答
    私信
满意答案
查看全部

1个回复