tags:

views:

268

answers:

6

This is a bit of a crazy idea, but would there be a way to bootstrap all the functions that php have without using a helper function to call the functions.

What I mean is actually calling some code or function before and after every basic function call.

Example:

<?php
echo "Hello";
?>

But these functions would be called before echo and after echo:

<?php
function pre_echo()
{
  do_something();
}

function post_echo()
{
  do_something_else();
}
?>
A: 

Just as an FYI, your example picked something which isn't actually a function in the strictest sense, echo is a language construct and handled differently by the PHP internals.

I think there's only two ways to achieve what you are after though:

  • Preprocess your PHP code to augment it with overridden function calls
  • or, modify the PHP internals to support hooking all function calls
Paul Dixon
You are quite right, I did not think directly about what I was using as an example. The idea of modifying the internals was something I thought about, but keeping up with the php versions would have been a pain.Thank you
Botto
+4  A: 

"Bootstrapping" is not the correct term here - what you want to do is called Aspect-oriented Programming, and yes, it can be done in PHP. However, it seems to require substantial changes to the way you normally use PHP.

Michael Borgwardt
Thank you, I was unsure of the term to use.
Botto
+1  A: 

Why do you want to do this? If you want to profile your code then use a profiler like Xdebug

codeassembly
Crazy security ideas I have, but thank you. I did not know of xdebug
Botto
A: 

This is why you write your own classes and encapsulate the functionality you want to have for that specific case there. Why make your stuff harder to understand for another programmer?

SchizoDuckie
A: 

No, you can't do this out of the box.

ahc
A: 

you can do this. you need change the php.ini file

at the directives

auto_prepend_file
auto_append_file

for example

auto_prepend_file=/home/php/run_before.php
auto_prepend_file=/home/php/run_after.php

then

<?php echo('hi'); ?>

the output will be:

# something before
hi
# something after
Gabriel Sosa