By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,948 Members | 838 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,948 IT Pros & Developers. It's quick & easy.

Dynamically load functions

P: n/a
Is there an option in php to do a 'require xxx.php' if, when a function
call to xxx is encountered, it is not defined? It would look in all the
standard places.
Jun 26 '06 #1
Share this Question
Share on Google+
7 Replies


P: n/a
Bob Stearns wrote:
Is there an option in php to do a 'require xxx.php' if, when a
function call to xxx is encountered, it is not defined? It would look
in all the standard places.


The best you can do is to test if the function exists and include its
definition when it doesn't:

if (!function_exists('somefunction')) {
require 'funcdef.php';
}

You can also use require_once/include_once to prevent the file being
included more than once, which causes an error because of the re-definition.
JW
Jun 26 '06 #2

P: n/a
Extending the solution of Bob:

function callWithAutoInclude($functionName, $parameters) {
if (!function_exists($functionName)) {
require($functionName.'.php');
}
return call_user_func_array($functionName, $parameters);
}

the following:
callWithAutoInclude('somefunction', array($param1, $param2) );

will then be the equivalent of:
somefunction($param1, $param2);
Greetings,

Henk Verhoeven,
www.phpPeanuts.org

Janwillem Borleffs wrote:
Bob Stearns wrote:
Is there an option in php to do a 'require xxx.php' if, when a
function call to xxx is encountered, it is not defined? It would look
in all the standard places.

The best you can do is to test if the function exists and include its
definition when it doesn't:

if (!function_exists('somefunction')) {
require 'funcdef.php';
}

You can also use require_once/include_once to prevent the file being
included more than once, which causes an error because of the re-definition.
JW

Jun 26 '06 #3

P: n/a
There is also __autoload for use with classes, which is automatically
invoked if the class does not exist.

See http://us2.php.net/__autoload

function __autoload($class_name) {
require_once $class_name . '.php';
}

$obj = new MyClass1();
$obj2 = new MyClass2();

Henk Verhoeven wrote:
Extending the solution of Bob:

function callWithAutoInclude($functionName, $parameters) {
if (!function_exists($functionName)) {
require($functionName.'.php');
}
return call_user_func_array($functionName, $parameters);
}

the following:
callWithAutoInclude('somefunction', array($param1, $param2) );

will then be the equivalent of:
somefunction($param1, $param2);
Greetings,

Henk Verhoeven,
www.phpPeanuts.org

Janwillem Borleffs wrote:
Bob Stearns wrote:
Is there an option in php to do a 'require xxx.php' if, when a
function call to xxx is encountered, it is not defined? It would look
in all the standard places.

The best you can do is to test if the function exists and include its
definition when it doesn't:

if (!function_exists('somefunction')) {
require 'funcdef.php';
}

You can also use require_once/include_once to prevent the file being
included more than once, which causes an error because of the re-definition.
JW


Jun 26 '06 #4

P: n/a
Richard Levasseur wrote:
There is also __autoload for use with classes, which is automatically
invoked if the class does not exist.

See http://us2.php.net/__autoload

function __autoload($class_name) {
require_once $class_name . '.php';
}

$obj = new MyClass1();
$obj2 = new MyClass2();

Henk Verhoeven wrote:
Extending the solution of Bob:

function callWithAutoInclude($functionName, $parameters) {
if (!function_exists($functionName)) {
require($functionName.'.php');
}
return call_user_func_array($functionName, $parameters);
}

the following:
callWithAutoInclude('somefunction', array($param1, $param2) );

will then be the equivalent of:
somefunction($param1, $param2);
Greetings,

Henk Verhoeven,
www.phpPeanuts.org

Janwillem Borleffs wrote:

Bob Stearns wrote:
Is there an option in php to do a 'require xxx.php' if, when a
function call to xxx is encountered, it is not defined? It would look
in all the standard places.

The best you can do is to test if the function exists and include its
definition when it doesn't:

if (!function_exists('somefunction')) {
require 'funcdef.php';
}

You can also use require_once/include_once to prevent the file being
included more than once, which causes an error because of the re-definition.
JW



Thanks for the ideas guys.

I was trying to get my functions to behave like builtin functions; be
there when they are called. Right now I am including my whole library in
the initialization procedure every module uses, which, while very
wasteful of processor resources, lets me use my functions on an ad hoc
basis without remembering if I've included them; this is especially
important in the maintenance portion of the life cycle of a module.
Another possible choice, between dynamic loading and REQUIREing unneeded
modules, would be a REQUIRE IF NEEDED statement which would create a
table the compiler would use to include the file if a function call to
the same name was invoked.
Jun 26 '06 #5

P: n/a

Bob Stearns wrote:
Richard Levasseur wrote:
There is also __autoload for use with classes, which is automatically
invoked if the class does not exist.

See http://us2.php.net/__autoload

function __autoload($class_name) {
require_once $class_name . '.php';
}

$obj = new MyClass1();
$obj2 = new MyClass2();

Henk Verhoeven wrote:
Extending the solution of Bob:

function callWithAutoInclude($functionName, $parameters) {
if (!function_exists($functionName)) {
require($functionName.'.php');
}
return call_user_func_array($functionName, $parameters);
}

the following:
callWithAutoInclude('somefunction', array($param1, $param2) );

will then be the equivalent of:
somefunction($param1, $param2);
Greetings,

Henk Verhoeven,
www.phpPeanuts.org

Janwillem Borleffs wrote:
Bob Stearns wrote:
>Is there an option in php to do a 'require xxx.php' if, when a
>function call to xxx is encountered, it is not defined? It would look
>in all the standard places.
>
The best you can do is to test if the function exists and include its
definition when it doesn't:

if (!function_exists('somefunction')) {
require 'funcdef.php';
}

You can also use require_once/include_once to prevent the file being
included more than once, which causes an error because of the re-definition.
JW



Thanks for the ideas guys.

I was trying to get my functions to behave like builtin functions; be
there when they are called. Right now I am including my whole library in
the initialization procedure every module uses, which, while very
wasteful of processor resources, lets me use my functions on an ad hoc
basis without remembering if I've included them; this is especially
important in the maintenance portion of the life cycle of a module.
Another possible choice, between dynamic loading and REQUIREing unneeded
modules, would be a REQUIRE IF NEEDED statement which would create a
table the compiler would use to include the file if a function call to
the same name was invoked.

I was thinking somet a bit more like this:

function __autoload($c) {
switch($c) {
case 'Utility':
include('Utility.class.php');
}
}

overload('Utility');

class Utility {
public static $includeMap = array('myfunc'=>'myfunc.function.php');

public function __call($m,$a,&$r) {
if(!function_exists($m)) {
include(self::$includeMap[$m]);
}
$r = call_user_func_array($m,$a);
return true;
}
}
then you do:
Utility::myfunc();

Of course, you'd have to prefix all the function calls with Utility or
what not.

Jun 27 '06 #6

P: n/a
Thank you. This something along the lines I was thinking. I still have
to change every function invocation, but only minimally, and if I must
then I must.

However I have some questions. I am an ancient dinosaur (retired after
more than 40 years of programming and related activities) and have not
played with the OOP features of PHP.

1 where is the (privileged, I would think, starting with '__') function
'__autoload' referenced?

2 $includeMap should contain 1 entry for each function I wish to
reference, right? Why is it not private instead of public?

3 Is the function '__call' an override of a virtual function which is
used to do the actual calling of functions in objects, after converting
the argument list to an array (merging default parameters? )?

4 Is the function 'call_user_func_array' the actual system function
calling routine?

5 A matter of curiosity: are the system built in function arranged in
classes that should be over ridden like this for purposes of
instrumentation or further argument validation?
I was thinking somet a bit more like this:

function __autoload($c) {
switch($c) {
case 'Utility':
include('Utility.class.php');
}
}

overload('Utility');

class Utility {
public static $includeMap = array('myfunc'=>'myfunc.function.php');

public function __call($m,$a,&$r) {
if(!function_exists($m)) {
include(self::$includeMap[$m]);
}
$r = call_user_func_array($m,$a);
return true;
}
}
then you do:
Utility::myfunc();

Of course, you'd have to prefix all the function calls with Utility or
what not.

Jun 27 '06 #7

P: n/a

Bob Stearns wrote:
Thank you. This something along the lines I was thinking. I still have
to change every function invocation, but only minimally, and if I must
then I must.

However I have some questions. I am an ancient dinosaur (retired after
more than 40 years of programming and related activities) and have not
played with the OOP features of PHP.

1 where is the (privileged, I would think, starting with '__') function
'__autoload' referenced?

Its a built in PHP function. www.php.net/__autoload
2 $includeMap should contain 1 entry for each function I wish to
reference, right? Why is it not private instead of public?

You could make it private, if you wanted. Doesn't really matter.
3 Is the function '__call' an override of a virtual function which is
used to do the actual calling of functions in objects, after converting
the argument list to an array (merging default parameters? )?

__call is automatically invoked when you overload() and object, see
www.php.net/overload
4 Is the function 'call_user_func_array' the actual system function
calling routine?

Yes.
5 A matter of curiosity: are the system built in function arranged in
classes that should be over ridden like this for purposes of
instrumentation or further argument validation?


Huh? Not sure what you mean. Do you mean the built in php functions?
I don't think they are part of any pre-existing class. Some things are
language constructs (echo, unset, etc) and can't be called with
call_user_func_array.

As for argument validation...all it does is pass them to the function,
if its missing arguments or has arguments of the wrong type, it'll give
an error/warning like it normally would.

It should be noted that there are a few limitations due to __call and
call_user_func, namely you can't return things by reference. You may
be able to overcome this limitatin by wrapping the returned reference
inside an array, though.

Jun 27 '06 #8

This discussion thread is closed

Replies have been disabled for this discussion.