Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

[PHP] Exporting a function from a .php file

This topic is 5233 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I would like to call a function contained within a .php file, WITHOUT INCLUDING THE .php file - I have heard you can do this, possibly with classes? Any help is appreciated.

Share this post


Link to post
Share on other sites
Advertisement
I''m at a loss as to why you would want to do such a thing. I''ve only been programming in PHP since November of ''03, so maybe I wouldn''t see the practical application of using a function from another PHP file without including it... maybe you could clarify this for me.

Otherwise, see if any of these functions may be of help to you:
PHP: Function Handling functions

Good luck.

___________________
Sketchworx Studios

Share this post


Link to post
Share on other sites
Why the importance of not including the file? That seems to be the most viable route. The only other option I see is if there is a way to create COM Components in PHP and then access them from another PHP script through the CLSID.

Share this post


Link to post
Share on other sites
I had heard you could do it...that way, in my thinking, you could simply call the function from the file without including the entire thing (i.e. any output preformed in the main body of the included file would be ignored).

Share this post


Link to post
Share on other sites
My approach is usually to create the main body of the script (perhaps index.php) and any functions/classes I need, I split up among different files. These files only include functions, nothing in the "body" of the script. This way, I have several "libraries" of functions for whatever I need.

I don''t know of a way to call a single function from another PHP file. I''ve only been doing PHP since November, but that''s all I''ve been doing and I haven''t seen anything that really supports what you want to do in the manual aside from that link from my last post...

___________________
Sketchworx Studios

Share this post


Link to post
Share on other sites
Sorry, let me clarify. It's 1:35am (EST here) and I'm not wording myself too well. Here's a basic example of what I usually do...
Say I wanted to create a class that contains a bunch of functions that does GodKnowsWhat...

<?php
class GodKnowsWhat {
var $somevariable;
...other variables...

function GodKnowsWhat () { ...insert constructor code.. }
...member functions...
}
?>

I'll just insert that block of code into its file. Perhaps 'onlygodknows.php'. If anyone were to run that script, nothing would happen. There's nothing in the "body" or whatever the formal name of it is... Then, in the script I'm running (which is going to call create an instance of this class... perhaps index.php) would just include that PHP file and create a GodKnowsWhat object

...for example...

<?php
include ('onlygodknows.php');
$whatdoesgodknow = new GodKnowsWhat();
$meaningoflife = $whatdoesgodknow->PleaseTellMe();
?>

Man, I need to be more serious with my code samples... anyways, I think you get the gist of it. I never really place code for functions in the same file as the main routine unless its like two functions and no other script needs them. Otherwise, I just throw them in another script and call it a "library" heh heh

I suggest you take the practical route, because "The only other option I see is if there is a way to create COM Components in PHP and then access them from another PHP script through the CLSID" sounds like an awful lot of trouble when you can circumvent it easily by throwing your functions alone in their own script.

I hope that made sense...

___________________
Sketchworx Studios

[edited by - sketch on January 23, 2004 2:01:44 AM]

Share this post


Link to post
Share on other sites
The only other way I can think of doing it is to read the file into a string, then manipulate the string until only the class definition is left. You can then eval() the string as PHP code. Yuk.

That''s not a good way of doing it at all - if a function needs to be reused put it in a separate script like people have already said.

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!