Make A Custom Function Available In The Entire Magento System

One of my co-workers recently started working with Magento. Until recently he was primarily working in a custom system we had developed.  One of the functions that he was very used to using was a debug function that simply was a wrapper for PHP’s print_r() that echoed out the beginning and ending pre tags.

Now this was not a show stopped for him, but it quickly became irritating for him to not have quick function available to him in Magento (especially since Magento has some massive objects) and he forgot to put in his pre tags.

Since I am a nice guy, I decided to see if there was a way that I could bring this little function into the system for him and it was easier than I thought it would have been.

In order to add a function that is available in every part of the Magento system you have to remember how a request flows through Magento. Everything starts in the index.php file. All I had to do was add this function to this file and it was available everywhere.

Now a few things to consider when doing this.

  • I would not do this in production (it probably would not hurt in this case, but you never know)
  • In terms of Magento coding standards, I would call this a little “dirty” the more “correct” way to do this would be to put this code somewhere in a header in a module.

If you are curious what the actual function looked like I have posted it below:

function print_array($var, $echo=false) {
  echo "<pre>";
  $str = print_r($var, $echo)
  echo "</pre>";
  return $str;
}

So there you have it. A quick and dirty way to bring in your own functions to the entire Magento system.

Leave a Reply

Your email address will not be published. Required fields are marked *