What does this line of code do?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
fluvly
Forum Newbie
Posts: 10
Joined: Wed Sep 23, 2009 10:08 am

What does this line of code do?

Post 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.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: What does this line of code do?

Post 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'));
Post Reply