473,804 Members | 4,272 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Dynamically load functions

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
7 1878
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_exis ts('somefunctio n')) {
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
Extending the solution of Bob:

function callWithAutoInc lude($functionN ame, $parameters) {
if (!function_exis ts($functionNam e)) {
require($functi onName.'.php');
}
return call_user_func_ array($function Name, $parameters);
}

the following:
callWithAutoInc lude('somefunct ion', array($param1, $param2) );

will then be the equivalent of:
somefunction($p aram1, $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_exis ts('somefunctio n')) {
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
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($cla ss_name) {
require_once $class_name . '.php';
}

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

Henk Verhoeven wrote:
Extending the solution of Bob:

function callWithAutoInc lude($functionN ame, $parameters) {
if (!function_exis ts($functionNam e)) {
require($functi onName.'.php');
}
return call_user_func_ array($function Name, $parameters);
}

the following:
callWithAutoInc lude('somefunct ion', array($param1, $param2) );

will then be the equivalent of:
somefunction($p aram1, $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_exis ts('somefunctio n')) {
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
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($cla ss_name) {
require_once $class_name . '.php';
}

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

Henk Verhoeven wrote:
Extending the solution of Bob:

function callWithAutoInc lude($functionN ame, $parameters) {
if (!function_exis ts($functionNam e)) {
require($functi onName.'.php');
}
return call_user_func_ array($function Name, $parameters);
}

the following:
callWithAutoInc lude('somefunct ion', array($param1, $param2) );

will then be the equivalent of:
somefunction($p aram1, $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_exis ts('somefunctio n')) {
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

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($cla ss_name) {
require_once $class_name . '.php';
}

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

Henk Verhoeven wrote:
Extending the solution of Bob:

function callWithAutoInc lude($functionN ame, $parameters) {
if (!function_exis ts($functionNam e)) {
require($functi onName.'.php');
}
return call_user_func_ array($function Name, $parameters);
}

the following:
callWithAutoInc lude('somefunct ion', array($param1, $param2) );

will then be the equivalent of:
somefunction($p aram1, $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_exis ts('somefunctio n')) {
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('Utilit y.class.php');
}
}

overload('Utili ty');

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

public function __call($m,$a,&$ r) {
if(!function_ex ists($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
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('Utilit y.class.php');
}
}

overload('Utili ty');

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

public function __call($m,$a,&$ r) {
if(!function_ex ists($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

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...al l 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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
2203
by: Randy Webb | last post by:
This page: http://www.hikksworld.com/loadJSFiles/ Is one where I was testing the ability of browsers that can/can't dynamically load a JS file with one of the three methods: 1)Changing the src property of a script tag. 2)Inserting a script src="" tag in a div via innerHTML 3)Using document.createElement to add a script element. The fourth method on the page is writing a script tag to a layer, and to the best of my knowledge it only...
2
2548
by: Claire | last post by:
How do I load and run dll functions dynamically please? The dll may be anywhere and its location is set in a command line parameter. I've loaded the dll OK, done the equivalent of GetProcAddress // kernel32, not used dynamically as it's fixed location. internal static extern IntPtr LoadLibrary(String dllname); internal static extern IntPtr GetProcAddress(IntPtr hModule, String
2
6837
by: J.Marsch | last post by:
I'm pretty new to ASP.Net (server side) Suppose that I have an instance of an ASP Page class, and I want the client's web browser to load it. I don't necessarily know the URL of the page that I have, I just have an instance of it. How do I make the browser load the page. If I were writing winform code, it would be something like this:
8
4318
by: Donald Xie | last post by:
Hi, I noticed an interesting effect when working with controls that are dynamically loaded. For instance, on a web form with a PlaceHolder control named ImageHolder, I dynamically add an image button at runtime: //----- Code snippet protected System.Web.UI.WebControls.PlaceHolder ImageHolder; private void Page_Load(object sender, System.EventArgs e)
2
2223
by: stroumf | last post by:
Hey, My problem is the following: I need to use functions from different javascripts and I don't want to load all the javascripts initially. So I need to load the scripts on demand. Now I know how I can load a script dynamically: function loadScript(scriptname) { var e = document.createElement("script"); e.src = scriptname+".js";
6
11091
by: | last post by:
I have made some user controls with custom properties. I can set those properties on instances of my user controls, and I have programmed my user control to do useful visual things in response to how those properties are set. I want to be able to do two other things: a) add User control instances to my page, filling in the place of placeholder controls, and b) programmatically setting custom properties on those dynamically spawned...
0
1551
by: has | last post by:
Hi all, need a little bit of advice on dynamically binding an embedded Python interpreter. First, the code for anyone that wants a look: http://trac.macosforge.org/projects/appscript/browser/py-osacomponent/trunk/PyOSA It's a Python OSA component for OS X. The idea is that you wrap up a scripting language interpreter as a Carbon Component Manager component (similar to writing a COM component); applications can then call the
2
1633
by: nashtm | last post by:
Hi All I am making the transition to client side programming and am new at JS. I am using YUI's connection manager to dynamically load forms - what I don't know is how to load the accompanying JS functions that does stuff specific to that form. Must those functions reside in the parent or can they be loaded just like the form itself is loaded. In the example below I don't know how to load the JS postForm() function Thanks for your...
20
4306
by: Nickolai Leschov | last post by:
Hello all, I am programming an embedded controller that has a 'C' library for using its system functions (I/O, timers, all the specific devices). The supplied library has .LIB and .H files. How can I dynamically load a LIB file and access all its functions? Surely someone has solved similar task? My intention is to use a Forth system for programming the controller,
4
2689
by: =?Utf-8?B?SmVzc2ljYQ==?= | last post by:
Hi All, I know that I can use the GetProcAddress to get the proc address of a global exported function from a WIn32 dll. But if I have an exported class in the dll, is there a way to dynamically load this dll and get an instance of this class? (No static linking with the lib file) I am currently thinking about having global exported functions which internally calls the member functions of the class's single instance. These global...
0
9710
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10593
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10340
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10329
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10085
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7626
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6858
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5527
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
3
3000
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.