How do I NOT autoload a class in php?

[亡魂溺海] 提交于 2019-12-07 14:13:47

问题


the autoloader at the beginning of my php code

function __autoload($class_name) {
    include_once $class_name . '.class.php';
}

is causing a call to new MongoClient(); to fail with the error Warning: include_once(MongoClient.class.php): failed to open stream

How can I use the autoloader for my classes and still use the standard classes?

Note: MongoDb has been installed with PECL and works fine with the autoloading function removed. mongo-1.3.0beta2 on php 5.4.9


回答1:


__autoload(), if defined, is called each time you try to access a class that has not been imported manually using require_once() or include_once() and is not part of the PHP internal classes.

In your case the __autoload() is triggered although you try to access a PHP internal class - MongoClient that is provided by the php-mongo extension. When you are not using __autoload() it works as expected.

It looks like the extension doesn't speak well with the PHP interpreter. You should first try an update from the beta to the stable 1.3.1 version. If this won't help, it will need further investigation.


Btw, if you try to instantiate a MongoClient object inside a namespace use \MongoClient(), like this:

namespace Foo;

$client = new \MongoClient();

The \ refers to the global namespace.




回答2:


PHP will not try to autoload anything that is in the stl (like PDO, mysqli, etc)

If MongoClient is not one of those types of classes, it will try to autoload it.

Make sure you have PECL installed




回答3:


As Neal pointed out, make sure you have the PECL mongo client installed and keep in mind that some times PHP uses a different php.ini file for the CLI.

If in CLI, you can get the correct php.ini by doing:

php -r "phpinfo();" | grep "php.ini"

So, after checking the PECL, make sure you have the extension=mongo.so in the correct php.ini



来源:https://stackoverflow.com/questions/13923447/how-do-i-not-autoload-a-class-in-php

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!