包含的文件何时被视为已执行


When is a included file considered executed?

包含的文件何时被视为已执行?

register_shutdown_function($callback[,$params])

注册要在脚本执行完成或exit()被称为后执行的回调

回调是在包含文件中的代码完成时执行的,还是在包含该代码的脚本完成时执行?

我考虑过PHP脚本究竟是如何执行的?

这个问题并不能回答解释器是将文件编译成一个大文件还是单独执行,无论是在演示中还是在答案中。

关于起源(感谢评论)。在magento中,Varien_Autoload类有一个构造函数:

public function __construct()
{
    register_shutdown_function(array($this, 'destroy'));//<- this is what the question is about
    $this->_isIncludePathDefined = defined('COMPILER_INCLUDE_PATH');
    if (defined('COMPILER_COLLECT_PATH')) {
        $this->_collectClasses  = true;
        $this->_collectPath     = COMPILER_COLLECT_PATH;
    }
    self::registerScope(self::$_scope);
}

该文件包含在Mage.php中。我想知道函数destroy是否在类定义完成后立即执行。经过思考,您可以看到函数无论如何都不会被调用(类定义不会实例化),但这并没有减少意义。

在阅读了include关键字后,我测试了这是如何工作的,当主脚本完成时,似乎认为脚本已执行。

考虑这个代码:

<?php 
$hello = 'Hello';
include 'world.php';

world.php

<?php
register_shutdown_function('world');
function world()
{
    echo ' world!';
}

输出为Hello world!