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 certain file types from directory listings

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

How to enable the rewrite engine in .htaccess?

A list of operators on Eloquent's where() method

How to hide the server signature (hide Apache info) from HTTP headers with .htaccess

How to fix the MySQL related "Specified key was too long error" error in Laravel

How to Enable Directory Listings in Apache with .htaccess

How to sort an array in javascript?

How do Invokable Controllers work in Laravel?

How to use subdomains as parameters in your routes (or in your web.php file)