Jump to content

[SOLVED] What does this line of code do?


fluvly

Recommended Posts

I found this line of code, but I can't understand the syntax of it:

 

register_shutdown_function(array($this, 'close'));

 

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:

 

array($this, 'close')

 

 

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.

 

Link to comment
https://forums.phpfreaks.com/topic/175228-solved-what-does-this-line-of-code-do/
Share on other sites

It calls a class method. The first element in the array identifies an instantiated class object ($this, the instantiated object in which the shutdown function is defined), the second element identifies the method by name ('close')

Thank you for the explanation, and now one side of it is clear. But I still don't understand why those 2 elements are put in an array... shouldn't there be just the name of the function? Something like:

 

register_shutdown_function($this->close);

 

It might be a banal doubt, but I'm new to php and I really can't understand the "array" bit. Thanks!

 

Thank you for the explanation, and now one side of it is clear. But I still don't understand why those 2 elements are put in an array... shouldn't there be just the name of the function? Something like:

 

register_shutdown_function($this->close);

It would simply be the name of a function if the code was procedural, but it's not - it's OOP. Using an array like this is the standard mechanism that PHP uses for a lot of callbacks when the method is defined within a class.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.