marcio
marcio

Reputation: 10512

Is it possible to add new functions to PHP?

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.

Upvotes: 2

Views: 250

Answers (4)

macjohn
macjohn

Reputation: 1803

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

Upvotes: 4

mario
mario

Reputation: 145482

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.)

Upvotes: 5

KingCrunch
KingCrunch

Reputation: 132011

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

Upvotes: 1

MitziMeow
MitziMeow

Reputation: 655

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

Read this (php.net manual)

auto-append-file and auto-prepend-file

Upvotes: 5

Related Questions