How could you include a custom function for every single time PHP runs?

PHP: How could you include a custom function for every single time PHP runs?

There are probably a few ways to do this, but one simple way would be to put your function in a file in /somedir/something.php. Then update php.ini and set auto_prepend_file = /somedir/something.php.

If you want to use a package (via composer) on all of your scripts you could also do something like composer global require THEPACKAGE/NAMEHERE, then do auto_prepend_file = ${HOME}/.composer/vendor/autoload.php

I would not recommend doing this though! The setup will work only on your current machine, and would cause problems with debugging or getting your scripts to work the same on other machines. I have very rarely used auto_prepend_file, and every time I have it has caused problems with deployment to other servers or for testing.
webdevetc profile pic
webdevetc

I am a 29 year old backend web developer from London, mostly focusing on PHP and Laravel lately. This (webdevetc.com) is my blog where I write about some web development topics (PHP, Laravel, Javascript, and some server stuff). contact me here.



More...


Comments and discussion about How could you include a custom function for every single time PHP runs?

Found this interesting? Maybe you want to read some more in this series?

Or see other topics in the PHP language

Or see other languages/frameworks:
PHP Laravel Composer Apache CentOS and Linux Stuff WordPress General Webdev and Programming Stuff JavaScript
Or see random questions

How to hide directory listing in .htaccess?

How to set the Expires header in .htaccess

How to split a string into an array, in JS

How to round a number in JS?

How can you check if a variable is alphanum (alphanumeric, letters and numbers only) in PHP?

How do you access the php://input stream?

How to show a specific view file for certain error codes?

How to find out what version of Laravel you are using?

What is the difference between var, let and const in Javascript?

How to review blog posts (written by other users) before they get published on your WordPress blog?