Looking for more information on how to do PHP the right way? Check out PHP: The Right Way

Evert Pot:
PHP's callable typehint too loose?
May 07, 2015 @ 15:19:56

In his latest post Evert Pot wonders if the current implementation of the "Callable" type in PHP is too loose when it comes to what it will accept as a valid callable resource.

PHP got support for closures in version 5.3, and in PHP 5.4 we got support for a callable typehint. [...] All these little changes make it feel more comfortable to apply functional programming concepts to PHP, but occasionally we need to drop back to using less aesthetically pleasing code.

In his examples of "less aesthetically pleasing code" he shows a few different methods that work that aren't the typical closure or object arguments (like passing in an array of object+method name). He also shows an interesting option where you can use a string with a static method call (ex: "MyClass::method") and it will still be accepted. He points out that for this to work correctly in all situations, the call_user_func method should be used, not just calling the input directly.

tagged: callable typehint loose object method array variable iscallable calluserfunc

Link: http://evertpot.com/on-callables-and-closures/

Lorna Mitchell's Blog:
Callbacks in PHP
Feb 14, 2011 @ 19:41:28

Lorna Mitchell has a new post to her blog today looking at a very handy piece of PHP functionality sprinkled around in different functions - using callbacks to handle complicated processing.

Recently I was working on something and I wanted to call an object method as a callback, but got confused when I realised the method had been caused statically. This was caused by my inability to RTFM and I wondered how I'd come so far without actually coming across the many and varied things you can pass into any place a callback is needed.

Besides the normal callback functions you can put in something like call_user_func, she also mentions something a bit more powerful - passing in an array that contains a pointer to an object and a method inside it. This ability allows you to keep your OOP encapsulation intact without having to make global functions. In PHP 5.3, there's even some of the PHP functions that use call backs that will allow you to use closures/anonymous functions without even having to make a separate function.

tagged: callback calluserfunc function object closure anonymous

Link:

SitePoint PHP Blog:
Dynamic global functions in PHP
Oct 22, 2007 @ 16:12:00

On the SitePoint PHP blog, there's a quick tutorial from Troels Knak-Nielsen about the creation and use of dynamic global functions in your PHP application:

Like many others, I prefer to use procedural PHP as a template language. While PHP's syntax makes it a practical choice for this, there is a problem with embedding dynamic content. [...] A single letter, regular function is undoubtedly the simplest way to extend PHP's syntax. Thinking about it, it's fairly obvious, but it just never occurred to me. [...] There is a problem though; Since this is such a good name for a function, chances are that someone else would use it for something different, or perhaps even for the same.

As a more viable solution, he recommends going dynamic and creating fumctions (via the call_user_func_* functions) and an addition to the $GLOBALS superglobal to make calling the custom function simpler.

tagged: dynamic global function calluserfunc superglobal dynamic global function calluserfunc superglobal

Link:

SitePoint PHP Blog:
Dynamic global functions in PHP
Oct 22, 2007 @ 16:12:00

On the SitePoint PHP blog, there's a quick tutorial from Troels Knak-Nielsen about the creation and use of dynamic global functions in your PHP application:

Like many others, I prefer to use procedural PHP as a template language. While PHP's syntax makes it a practical choice for this, there is a problem with embedding dynamic content. [...] A single letter, regular function is undoubtedly the simplest way to extend PHP's syntax. Thinking about it, it's fairly obvious, but it just never occurred to me. [...] There is a problem though; Since this is such a good name for a function, chances are that someone else would use it for something different, or perhaps even for the same.

As a more viable solution, he recommends going dynamic and creating fumctions (via the call_user_func_* functions) and an addition to the $GLOBALS superglobal to make calling the custom function simpler.

tagged: dynamic global function calluserfunc superglobal dynamic global function calluserfunc superglobal

Link:


Trending Topics: