Page 1 of 1

What does this line of code do?

Posted: Wed Sep 23, 2009 10:22 am
by fluvly
I found this line of code, but I can't understand the syntax of it:

Code: Select all

[color=#000080]register_shutdown_function([/color][color=#800000]array($this, 'close')[/color][color=#000080]);[/color]
I read on the online documentation that the register_shutdown_function calls a shutdown function before the script ends; it's not the actual function that I have a problem with, but the syntax of the shutdown function between parenthesis:

Code: Select all

[color=#800000]array($this, 'close')[/color]
What does that mean? What happens in this case when the register_shutdown_function is called? What function does it call? An array? I thank in advance anyone who is able to help.

Re: What does this line of code do?

Posted: Wed Sep 23, 2009 12:01 pm
by John Cartwright
That is how you pass an instance of the class and specify the method which you want to be called. In your case, it is the class your looking at registering it's own close() function as a shutdown function.

I.e.,

Code: Select all

class foobar()
 
{
   public function __construct() 
   {
       register_shutdown_function(array($this, 'close')); 
   }
 
   public function close()
   {
       //will be called later on scripts end
   }
}
Otherwise, if we were outside the class's scope, we could have done

Code: Select all

$myobject = new foobar();
register_shutdown_function(array($myobject, 'close'));