0

My problem is I need to execute a function every time I include a file.

But I know that if I use include inside a function, it will mess up.

So there is a way to change include function to execute a function inside? Or to create a "listener" that gets triggered if I use include?

Example:

function include_file($path){

    if($this->error)
         echo $this->error_msg;

    include_once($path);

}

Thanks!

Maurício Giordano
  • 3,116
  • 4
  • 32
  • 60

1 Answers1

0

But I know that if I use include inside a function, it will mess up.

IMHO, it won't mess up.

including_file.php

<?php

include_once ("included_file.php");
echo $test;
?>

included_file.php

<?php

function myFunction()
{
return "Testing 123";
}

$test=myFunction();

?>

This will call myFunction everytime this file is included. This sounds very trivial unless i misunderstood the question

alex
  • 479,566
  • 201
  • 878
  • 984
Hanky Panky
  • 46,730
  • 8
  • 72
  • 95
  • That would be completely unecessary... I would have to put the function into every file. – Maurício Giordano Mar 22 '13 at 03:39
  • Nopes you wont have to. Includes do not work like every include works on its own members only. If you call a function in any of the included files that has already been defined in any of them, it will work. – Hanky Panky Mar 22 '13 at 03:40
  • That is not what I meant... I have several files to include, but I never include more than one at a time. – Maurício Giordano Mar 22 '13 at 03:41
  • Yes but i think the question needs to put up a scenario here as to what it expects in output. Only then can one put up a related example – Hanky Panky Mar 22 '13 at 03:50