简体   繁体   中英

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.

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. Before I load the class this is true (loaded). yet if I try to make an instance of wattsjus it cannot be found.

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:

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. Though it cannot be found. If I let it keep going and create wattsjus again it will say the class has already been defined. 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. Really weird glitch/bug I'd have to say.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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