Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bootstraping all functions in PHP

Tags:

php

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();
}
?>
like image 973
Botto Avatar asked Nov 26 '25 10:11

Botto


2 Answers

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

like image 119
Michael Borgwardt Avatar answered Nov 27 '25 23:11

Michael Borgwardt


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
like image 43
Paul Dixon Avatar answered Nov 27 '25 23:11

Paul Dixon



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!