Is it possible to add new functions to PHP? - 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.

You could use a "Global include" (defined in php.ini)
Read this (php.net manual)
auto-append-file and auto-prepend-file

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

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

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

Related

Hook into runtime of scripts

I am writing a very simple timer-extension in PHP, and I'm wondering how to:
Hook into the runtime / get a function of my extension to be called when script execution starts/ends
Hook into specific functions, e.g., Pdo::query() at start and end.
The purpose is to time given functions or scripts, so that we may easily calculate the execution time etc. I am aware that I can achieve this in pure PHP with:
auto_prepend_file / auto_append_file
Using an extension that lets me hook into the (beginning of) functions.
But I am mostly interested in C/C++ solutions (in the PHP/Zend API). Perhaps there is even a better way of doing what I want?
I'm pretty sure that php doesn't have hooks for functions in extensions!
the only hooks you can set in an extension are when the extension is loaded for the first time in apache (MINIT) and everytime a request is recieved by the server (RINIT).
It might somehow be possible to replace a function with your own (i have read somewhere a while ago that you can replace zend engine's functions for parsing,compiling etc).
ps: you might also want to take a look at xdebug, i'm pretty sure that extension does some timing/profiling, you can look how it's done there!

Detect during compilation whether PHP is being compiled with a given extension

I am trying to build an extension for PHP. After following Sara Golemon's book I have a basic extension which I can compile as a shared module and, in addition, I can compile it statically along PHP itself.
Now I want to modify the PHP interpreter in order to intercept particular internal function invocations and communicate these calls to my extension. I want to do this only when my extension is statically compiled with PHP---the interpreter build process should otherwise generate an unmodified PHP binary. My understanding is that I should use the C preprocessor. However, to achieve my goal I need a preprocessor flag that will only be raised when PHP is configured to compile with my extension (i.e. ./configure --enable-myextension). Unfortunately, I cannot find such a flag nor one seems to be set by the configure script.
I should say here that I have tried setting preprossessor flags within my extension's code but this will not work. My extension is first touched late in the build process (i.e. roughly after the core of the interpreter) and the flags I set there are not active when the bulk of interpreter code is being compiled.
Any thoughts? Do the above sound reasonable?
My understanding is that I should use the C preprocessor.
Nope, you don't need that.
I need a preprocessor flag that will only be raised when PHP is configured to compile with my extension
Why would you want that? It would basically limit the functionality of your extension artificially, although it's possible to hook function calls no matter how your extension is compiled.
Do the above sound reasonable?
In my opinion, it's not reasonable. Please have a look at how AOP hooks function calls: https://github.com/AOP-PHP/AOP
If you need to hook more than just function calls, you need to reach down at the lowest level, the opcodes, by using zend_set_user_opcode_handler(). Please use lxr.php.net or similar tools (fgrep, etc) to find out where and how such handlers are used. I know laruence was working hard on an interesting extension last year here: http://svn.php.net/viewvc/pecl/taint/trunk/taint.c?view=markup so I would take that as the most "up to date" way of doing things as a reference, if anything has changed in the meanwhile.

Put PHP files toghether

Is there a tool to take a PHP file, with all its dependencies to other external PHP files, and create one, huge, final PHP file that includes them all?
Thanks
You don't really want to take a whole library and put it in a single file, because you end up loading a bunch of class definitions that might not even be needed by your script (i.e.: script A might need it, but not script B, however they end up loading it anyway).
PHP 5.3 (and a PECL extension for 5.2) introduced PHARs (PHP Archives), which works a little like JARs (Java equivalent):
$phar = new Phar('myLibrary.phar');
$phar->addFile('myClass.php');
Then you can do:
include_once('phar://myLibrary.phar/myClass.php');
I use it often and it is indeed very useful for quick software updates on client/production servers.
I've never heard of anything like that before, but maybe this could help... not really sure how it works http://webscripts.softpedia.com/script/PHP-Clases/Split-and-merge-files-19891.html
It is a new extension built-in PHP 5.3. You can read more about it here
http://www.php.net/manual/en/intro.phar.php

Is it possible to call C code from php

is it possible to call C function from 3rd party C library from a PHP5 script.
If so any link or how to do it please.
You can also write PHP extensions in C. Put very simply, an extension lets you write a function (or functions) in C, then call those functions from PHP.
This Zend article is a good introduction.
PHP is written in C, so if you have some C code that you want to be able to call in PHP, the best solution is to write a PHP extension, which will allow you to expose the functionality via some new php functions.
It's been a long time since I did this, but PHP has some tools to set up the skeleton of an extension. A good place to start is probably the official documentation
There is a way to do it, but it's probably not a very good idea. One could write a small native program around the library (an executable for your system, that is). Then, you could use the php system() function to call that program. Then, read the output you need (if any) off the standard output?
(there may be a better way, I'm not super-familliar with PHP, but at least this should work.)
If you want to execute C source-code, than it's impossible. But you can compile that code and run it from within PHP using system().

Is there a tool to convert php built-in functions to c implementation?

I'm curious about how some built in functions are implemented,but it's very time consuming to look it up directly in the source,is there a tool that can automate this?
EDIT
Or is there a tool that can debug into the c code that's actually executed?
Most (all?) of the functions that can be accessed from PHP are defined under the ext/ directory in the PHP source code. If you have a recursive search tool, search for PHP_FUNCTION - if you saved the results of that search into a text file, it would be a pretty good "index" for figuring out where a PHP builtin is defined.
The really core stuff is in ext/standard.
Some rare "functions" are implemented directly as opcodes in the Zend virtual machine that PHP compiles to, so there isn't a well defined C function as such. I think strlen is such a function, for instance.
About the debugging the C code that's executed, I suppose it's possible to use something like dbg ; you'll first have to recompile PHP with the --enable-debug mode, though.
For more informations, you can take a look at :
Building PHP for extension development
Generating a gdb backtrace
I've never used this to debug PHP itself, but I've used those two pages to generate some backtraces of a crash I had with an extension, and it worked OK, from what I remember.
As a sidenote : using a PHP compiled with --enable-debug, you might have to recompile some of the extensions you're using and change the way they're loaded (it's the case for Xdebug, for instance) ; and some other might just not work at all anymore.
I believe that you should take a look at this.
Facebook has developed a tool to convert PHP code into c++.
So I guess it can handle C as well to some extent.

Categories