Reply to Re: Dynamically load functions

Your name:

Reply:


Posted by Bob Stearns on 06/26/06 20:38

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.

[Back to original message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация