2

I would like to add a function to PHP so I can use it in any script that runs on my server. Is it possible to register a function like if it was native from PHP? Does PHP has some kind of configuration file where I can register new global / native functions?

There is no intentions to use it in production, I'm just curious on how to achieve this.

marcio
  • 10,002
  • 11
  • 54
  • 83

4 Answers4

5

You could use a "Global include" (defined in php.ini)

Read this (php.net manual)

auto-append-file and auto-prepend-file

MitziMeow
  • 635
  • 4
  • 13
  • that's exactly what I was looking for, answer accepted (it seems your answer was the first one ;) – marcio Dec 13 '11 at 18:08
5

You can write extensions in C or C++, in particular write or use library bindings with SWIG or FFI. But that's a bit effort, and only advisable if you meant compiled "native" functions.

The lazy option to add new core functions to PHP via config is the auto_prepend_file= php.ini setting. That allows to register a script that gets executed before everything else. (I use that for fixing magic quotes on some servers, or always having phpquery available for CLI testing.)

Community
  • 1
  • 1
mario
  • 144,265
  • 20
  • 237
  • 291
4

There is a full chapter in the manual http://www.php.net/manual/en/internals2.php devoted to writing extensions to php, with examples.

macjohn
  • 1,755
  • 14
  • 18
1

The easiest way is to write them in PHP and include them before your scripts is executed using

auto_prepend_file = /path/to/file.php

http://php.net/manual/en/ini.core.php

else you need to learn C and write a extension

KingCrunch
  • 128,817
  • 21
  • 151
  • 173