douzhi2760 2009-10-20 22:24
浏览 18

如何跳过__autoload?

I have a php file which has multiple classes in it. I noticed that __autoload is being called when I instantiate any of the classes, even after the 'package' file has been autoloaded. This is worrying me, because surely if the initial 'package' .php file has been loaded, it is unnecessary overhead for __autoload when the classes have already been loaded.

I probably made a hash of explaining that, so here is an example:

<?php
class Class1{};
class Class2{};
?>

Then in another file:

<?php
new Class1;
new Class2;
?>

__autoload will be used for both Class1 AND Class2's instantiation... even though they are housed in the same file.

Is there a way to get around this?

Sorry if my explanation isn't very good, I'd really appreciate any help or tips.

  • 写回答

2条回答 默认 最新

  • duande1146 2009-10-20 22:29
    关注

    PHP's autoload should only be called if a class doesn't exist. In other words, for the most basic example, it uses the same logic as:

    if( !class_exists("Class1") )
        require "path\Class1.php"; 
    

    If you are finding otherwise, I would check to be sure you are doing everything correctly and report a bug.

    From PHP.net/autoload (important documentation highlighted):

    In PHP 5, this is no longer necessary. You may define an __autoload function which is automatically called in case you are trying to use a class/interface which hasn't been defined yet. By calling this function the scripting engine is given a last chance to load the class before PHP fails with an error.

    Bug in formatting, but the emphasis was on "which hasn't been defined yet". "Defined" occurs when a class is compiled (in most cases when a file containing said class is included).

    评论

报告相同问题?

悬赏问题

  • ¥100 iOS开发关于快捷指令截屏后如何将截屏(或从截屏中提取出的文本)回传给本应用并打开指定页面
  • ¥15 unity连接Sqlserver
  • ¥15 图中这种约束条件lingo该怎么表示出来
  • ¥15 VSCode里的Prettier如何实现等式赋值后的对齐效果?
  • ¥15 流式socket文件传输答疑
  • ¥20 keepalive配置业务服务双机单活的方法。业务服务一定是要双机单活的方式
  • ¥50 关于多次提交POST数据后,无法获取到POST数据参数的问题
  • ¥15 win10,这种情况怎么办
  • ¥15 如何在配置使用Prettier的VSCode中通过Better Align插件来对齐等式?(相关搜索:格式化)
  • ¥100 在连接内网VPN时,如何同时保持互联网连接