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 set custom HTTP headers in Apache (by editing .htaccess)

How to access the Laravel's route parameters outside of the controller?

How to show (or log) all SQL queries executed by Laravel

How to install jpegtran for Linux CentOS

How to check if two strings are anagrams in PHP?

How to eager load multiple levels of relationships in Eloquent?

How to sort an array in javascript?

How to view all installed PHP modules?

Push vs Pop vs Shift vs Unshift

How to find an item by Primary Key in Eloquent, or throw a ModelNotFoundException exception if it isn't found?