0

When is a included file considered executed?

register_shutdown_function($callback[,$params])

Registers a callback to be executed after script execution finishes or exit() is called

Is the callback executed when the code inside the included file finishes or when the script that included it finishes?

I considered How exactly is a PHP script executed?

That question does not answer whether the interpreter compiles the files into one big file or execute them separately, neither in the presentation or the answers.

About origin(Thanks commenters). In magento Varien_Autoload class has a constructor:

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);
}

That file is included in Mage.php I was wondering if the function destroy was executed right after the class had finished being defined. After giving it some thought, you can see that the function would never get called anyway(class definition does not instantiate), but that doesn't reduce the meaning.

Community
  • 1
  • 1
Olavi Sau
  • 1,647
  • 13
  • 20
  • 1
    You could take a look at the sources. What do you mean by *"one big file"* vs. *"separately"*? Of course a script finishes after all includes have finished and then the original script itself. Wouldn't make sense otherwise. – m02ph3u5 Nov 28 '15 at 20:28
  • I was considering if there count be multiple "finishes" for functions like ´register_shutdown_function´ – Olavi Sau Nov 28 '15 at 20:29
  • 1
    There's never "multiple finishes". – m02ph3u5 Nov 28 '15 at 20:31
  • What do you mean by "considered executed"? The script is going to be executed from the first line to the last, taking into account any loops or conditionals. An include is just as if the included file was copied and pasted in the calling file. Or do you mean parsed? Because code might be parsed even if it's not executed (eg. I believe if there's a script error in unreachable code, parsing is still going to fail). So please clarify what you mean, and maybe also what you are trying to achieve. – laurent Nov 28 '15 at 20:32
  • @m02ph3u5 Yeah I concluded that with my test. – Olavi Sau Nov 28 '15 at 20:33
  • @this.lau_ Strictly executed not parsed, in relation to the function register_shutdown_function . I was wondering would it execute the callback when the containing files script concluded or when the script that executed it finished. – Olavi Sau Nov 28 '15 at 20:35
  • May I ask What issue do have that you talk about 'including scripts' and PHP shutdown in the same question? i.e. _What problem do you have that you want to solve?_ There may be a 'more robust' method than needing to know exactly how scripts are executed? – Ryan Vincent Nov 28 '15 at 20:51
  • @RyanVincent I was trying to understand how the magento autoloader works, in there was this code and I wasn't sure how it worked and since there is no clear answer anywhere, I decided to ask it, people seem to be taking it rather negatively. I don't really see a use case for having an "include finish" function, just curious. – Olavi Sau Nov 28 '15 at 20:56
  • 1
    @OlaviSau, ok I see, then I think you've answered your own question below. In any case, an include is nothing special, it's just a block of text being copied and pasted in the calling script. So in the end it's just as if you have a giant script with everything inside. And execution is simply finished at the last line, after which shutdown functions are executed. – laurent Nov 28 '15 at 20:57
  • 1
    Thanks for the clarification. Would you please put that in your question - It will help everyone to know that you are interested in 'magento' 'final' processing as well as PHP? – Ryan Vincent Nov 28 '15 at 20:58
  • 1
    The 'PHP' shutdown sequence is a list of functions that you can add to that are 'called when PHP exits'. Now, they better be 'valid' or it can be very confusing trying to debug it. But it is just more PHP. Seriously, keep it really simple and all will be fine. – Ryan Vincent Nov 28 '15 at 21:03
  • You will need a 'finish' function if you have open 'resources' that PHP doesn't automatically manage? I have no idea what the package does. – Ryan Vincent Nov 28 '15 at 21:10
  • Do you mean a function to execute at the end of the include? I could just write it as plain PHP, since it seems that PHP just reaches the code and executes it, basically the same result if the lines were added in. – Olavi Sau Nov 28 '15 at 21:17
  • Yes, Hmm... Have you looked at the manual for the 'PHP shutdown sequence': You have to be careful but, yes - it is just PHP. Maybe interesting? [register_shutdown_function — Register a function for execution on shutdown](http://php.net/manual/en/function.register-shutdown-function.php). Also: [Explanation of: register_shutdown_function](http://stackoverflow.com/questions/13499399/explanation-of-register-shutdown-function) – Ryan Vincent Nov 28 '15 at 21:25
  • @RyanVincent Oh wow thanks :) Haven't seen the second one ;) – Olavi Sau Nov 28 '15 at 21:57

1 Answers1

0

I tested how does this work after reading about the include keyword and it seems that the script is considered executed, when the main script finishes.

Consider this code:

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

world.php

<?php
register_shutdown_function('world');

function world()
{
    echo ' world!';
}

The output will be Hello world!

Olavi Sau
  • 1,647
  • 13
  • 20
  • 1
    ... after the main script is finished and all resources are cleaned up, database connections closed and so on... – Hasse Björk Nov 28 '15 at 20:30
  • Thanks, I really didn't find a concise answer to this, the closest one is "How exactly is a PHP script executed?" That talks about how it is compiled into machine code not how it's internals work. – Olavi Sau Nov 28 '15 at 20:32