简体   繁体   English

自动加载模糊

[英]Autoload Obscurity

I"m sorry now but I don't know if I can get all the details possible to make an educated guess on what exactly is going on. 我很抱歉,但是我不知道我是否能获得所有可能的细节,以便对到底发生了什么进行有根据的猜测。

I'm trying to check to see if a class exists, if it doesn't I need to create the class. 我试图检查是否存在一个类,如果不存在,则需要创建该类。 Some classes have sub classes (namespaces). 一些类具有子类(命名空间)。

namespace {
    class wattsjus {
    }
}
namespace wattsjus {
    class OtherClass {
    } 
}

so whether I __autoload wattsjus or wattsjus\\OtherClass I'm loading both classes. 因此,无论我__autoload wattsjus还是wattsjus \\ OtherClass,我都在加载这两个类。

I can tell that up until the point I load the classes they are not there if I try and autoload the 'inner class'. 如果可以尝试自动加载“内部类”,那么直到加载类之前,我都可以告诉他们。 After I load the class I get an error that the class is already loaded. 加载类后,我得到一个错误,表明该类已被加载。 Yet I can print text just before that point and that line is never reached. 但是我可以在该点之前打印文本,并且永远不会到达该行。

I am using class_exists to make sure the wattsjus class is not loaded. 我正在使用class_exists来确保未加载wattsjus类。 Before I load the class this is true (loaded). 在加载类之前,这是真的(已加载)。 yet if I try to make an instance of wattsjus it cannot be found. 但是,如果我尝试创建wattsjus实例,将无法找到它。

I'm using the following code to check where the class was created and it's stating a line number that is never reached at this point. 我正在使用以下代码检查类的创建位置,并指出此时从未到达的行号。 I have an print statement before this line to be sure. 为了确保这一点,我在这行之前有一份打印声明。

$reflector = new ReflectionClass('wattsjus');
echo $reflector->getFileName();

A great mystery to me as to how code could have been reached without executing the previous line. 对于我来说,在不执行上一行代码的情况下如何达到代码是一个很大的谜。

Edit, Warning this is ugly code: 编辑,警告这是丑陋的代码:

function __autoload($class_name) {
    echo 'in';
    $parts = explode('\\',$class_name);
    if($class_name == $_GET['u5u'] || $parts[0] == $_GET['u5u']) {
        $cl = class_exists($_GET['u5u'], false);
        echo $cl ? 'yes' : 'no';
        if(!$cl) {
            if($_GET['e5e'] != 'Prod') { $env = $_GET['e5e']; }
            $q = "SELECT  f.`Name`, f.`$env"."ParameterList` `ParameterList` FROM `Function` f WHERE f.UserId = ".SiteUserId." AND `Status` = 'A' ORDER BY INSTR(f.`Name`, '\\\\') > 0, f.`Name`";
            $results = DataObject::GetDBObject()->Query($q, 'wattsjus_json');
            $class = "namespace { class $_GET[u5u] {";
            $class .= GetFunctions($results);
            $class .= '}}';
            echo 'I\'m not here yet';
            eval($class);
        } else {
            $reflector = new ReflectionClass('wattsjus');
            echo $reflector->getFileName();
        }

here the output that is produced trying to get OtherClass is: 在这里,试图获取OtherClass的输出是:

inyes/home4/wattsjus/public_html/ajson/global_base.php(25) : eval()'d code<br />
<b>Fatal error</b>:  Class 'wattsjus\OtherClass' not found in <b>/home4/wattsjus/public_html/ajson/global_base.php(112) : runtime-created function</b> on line <b>1</b><br />

so to me this is saying that the class wattsjus already exists which should include OtherClass in the namespace. 所以对我来说,这就是说wattsjus类已经存在,应该在名称空间中包含OtherClass。 Though it cannot be found. 虽然找不到。 If I let it keep going and create wattsjus again it will say the class has already been defined. 如果我继续进行下去并再次创建wattsjus,它将说该类已经定义。 Very confusing I know. 我很困惑。

I changed the structure of the classes to use "namespace wattsjus\\OtherClass" rather than make a OtherClass class and for some reason PHP is less confused by this. 我更改了类的结构,以使用“名称空间wattsjus \\ OtherClass”,而不是创建OtherClass类,并且由于某些原因,PHP对此不太困惑。 Really weird glitch/bug I'd have to say. 我不得不说真的很奇怪的故障/错误。

namespace {
    class wattsjus {
    }
}
namespace wattsjus\OtherClass {
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM