Reputation: 1622
I would like to access some functions defined in another PHP file, but that file also outputs content that I don't want to display.
Is there any way to access the functions without "running" the whole file? Perhaps by redirecting the output from that file to somewhere hidden?
Upvotes: 3
Views: 1752
Reputation: 33
If, like me, you have a situation where you want the php files to act independently, you can also use a $_SERVER and if statement...
In short, I have a bunch of roleplaying tools that I'm creating. One randomly generates a quest-giving NPC, questg.php. I have another page that creates quests, questc.php.
questg.php has two functions, one creates the quest givers stats (I want to share this) and the other displays those stats as an HTML snippet that gets called if a browser goes to questg.php.
questc.php also has two functions. One that creates a randomly generated quest and another that formats it as an HTML snippet, etc. as above EXCEPT, as part of the presentation, it calls "create_quest_giver" from questg.php as part of the presentation. It doesn't need "show_quest_giver" as it displays the giver in it's own way.
Both pages call another file called utility.php that holds all my frequently used functions (debugging tools, etc.).
So, all the share-able functions and loaded data gets put at the start of the php file. Then I use (e.g. for questg.php)
if (strpos($_["PHP_SELF"], 'questg.php')) {
include 'utility.php';
/* local code for displaying generated quest giver that only gets used if you're browsing this page, etc. */
?>
<HTML>
<!-- Rest of the web page -->
</HTML>
<?php } ?>
This has the advantage of only having to write/maintain/update the code once, I know where the code is because each tool is distinct in its purpose and thanks to the if statement, the webpage only gets generated if the page itself is browsed. The ob_* functions look very handy but this might also be useful if you're looking for a way for subsidiary pages to supply site content as well as support to other parts of the site.
Upvotes: 0
Reputation: 3722
The answers involving output buffering are correct, but you're solving the wrong problem at a rather fundamental level.
Grab those functions that are useful and move them to a different file. Then include that file from both the one you're working on now, and the one where they currently reside. Everything works, no weird hacks with output buffering.
Upvotes: 2
Reputation: 31477
<?php
ob_start();
require 'filename.php';
ob_end_clean();
?>
Upvotes: 4
Reputation: 449843
You could sneakily buffer the include's output using ob_start() and drop it using ob_end_clean(). This works only if the script doesn't flush the output buffer itself.
The better way would be to extract the needed functions from the include, and put them into a separate file.
Upvotes: 1
Reputation: 49396
Well, you could capture the output of the file using, say, an Output Buffer then discarding it. However, any other global side effects (global variables set, files written, etc) would still occur. It would be much better to refactor that file to move the functions into their own include, which can be include
ed by both.
Upvotes: 0
Reputation: 26902
Eh you might be able to do something with using the output buffer and manually clearing contents, but why not just move the common functions into a common include? I usually shove common helper methods into their own file (which won't output anything).
Upvotes: 3