我如何不自动加载php中的类? [英] How do I NOT autoload a class in php?

查看:50
本文介绍了我如何不自动加载php中的类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的php代码开头的自动加载器

the autoloader at the beginning of my php code

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

导致对new MongoClient();的调用失败,并显示错误 Warning: include_once(MongoClient.class.php): failed to open stream

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?

注意:MongoDb已与PECL一起安装,并且在删除自动加载功能后可以正常工作. php 5.4.9上的mongo-1.3.0beta2

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

推荐答案

__autoload()(如果已定义).不是PHP内部类的一部分.

__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.

在您的情况下,尽管您尝试访问由php-mongo扩展提供的PHP内部类-MongoClient,也会触发__autoload().当您不使用__autoload()时,它会按预期工作.

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.

该扩展名似乎无法与PHP解释器一起使用.您首先应该尝试从Beta更新到稳定的1.3.1版本.如果这样做没有帮助,则需要进一步调查.

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.

顺便说一句,如果您尝试在命名空间中实例化MongoClient对象,请使用\MongoClient(),如下所示:

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

namespace Foo;

$client = new \MongoClient();

\是指全局名称空间.

这篇关于我如何不自动加载php中的类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆