Register_shutdown_function () php

I am new to php. I found out about register_shutdown_function () in php. I read about this function in the php.net manual when the script completes the execution of this function. But I have a question

What if I put this function in a namespace, for example

 namespace MyNamespace;
 register_shutdown_function("myHandler");  // this throws an error because it can't find function
 function myHandler() 
 { 
    // Some Code.
 }


 namespace MyNamespace;
 register_shutdown_function("MyNamespace\myHandler");  // this throws no error.
 function myHandler() 
 { 
    // Some Code.
 }

why is this happening both register_shutdown_function () and myHandler () are in the same namespace ??

and if I put this namespace in another file, and I will include this file, but I will not use this namespace "MyNamespace", and then will it do it?

+4
source share
2 answers

, register_shutdown_function() . , PHP . . :

namespace MyNamespace;
$data = ['foo', 'bar'];
$data = array_filter($data, 'myHandler'); //warning, while 'MyNamespace\myHandler' is ok
register_shutdown_function("MyNamespace\myHandler");
function myHandler() 
{
        return 1;
}

-i.e. , - . , , (.. ) - , .

+5

, , , , :

...

<?php
function shutdown()
{
    // This is our shutdown function, in 
    // here we can do any last operations
    // before the script is complete.

    echo 'Script executed with success', PHP_EOL;
}

register_shutdown_function('shutdown');
?>
+1

All Articles