Reputation: 32232
I keep running into issues where I want to use a useful function like str_getcsv()
or quoted_printable_encode()
, but find that it only came into being between as of PHP 5.3. I want to have this code I'm writing be compatible with at least 5.2, but I don't want to have to write a bunch of specialized code.
I've gotten into the habit grabbing stand-in functions from the PHP.net comments, storing them in helpfully-named files like func.quoted_printable_encode.php
, and writing a block like the below in each place I want to invoke them.
if( ! function_exists('quoted_printable_encode') ) {
$funcfile = 'func.quoted_printable_encode.php';
if( file_exists($funcfile) && is_readable($funcfile) ) {
require('func.quoted_printable_encode.php');
} else {
Throw new Exception('Cannot invoke function: quoted_printable_encode');
}
}
This seems eerily similar to __autoload()
for classes, and __call()
for object methods, but I can't find anything regarding global functions. Does such a thing exist, or do I have to shoehorn all these extra functions into a header file somewhere?
Upvotes: 0
Views: 49
Reputation: 13283
There is no way to autoload functions in PHP. There is an RFC but it has not been implemented (and it probably will not be).
If you really want to autoload functions then it may make sense to create a helper class where you implement the functions as static methods. It is quite common in frameworks to do it like that.
Upvotes: 1