Including PHP functions from another file

前端 未结 5 2030
失恋的感觉
失恋的感觉 2021-01-20 02:01

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 f

相关标签:
5条回答
  • 2021-01-20 02:22
    <?php
    ob_start();
    require 'filename.php';
    ob_end_clean();
    ?>
    
    0 讨论(0)
  • 2021-01-20 02:31

    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.

    0 讨论(0)
  • 2021-01-20 02:32

    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 includeed by both.

    0 讨论(0)
  • 2021-01-20 02:37

    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.

    0 讨论(0)
  • 2021-01-20 02:42

    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).

    0 讨论(0)
提交回复
热议问题