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 order a Laravel hasMany relationship?

How to hide certain file types from directory listings

How to exclude URLs from the Laravel CSRF protection?

What is Bubble sort, and how to write an implementation of it in PHP

What are generators in PHP, and how do they compare to arrays?

How to add and remove items to an array in JS?

What is the spaceship operator (<=>) in PHP?

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

How to convert a string to an array of it's characters in PHP?

How to round a number in JS?