Making C functions available to php - php

I have a C dll containing functions and enumerations I want to make accessible from php.
How can I do it?

You need to write an extension. You do need some C experience but this tutorial is pretty easy to follow.
edit I googled around out of curiosity, apparently you can sort-of dynamically load a dll from php using w32api. I'd still go for the extension ;-)

Related

Loading php.ini settings for a PHP Swig

I'm working on a PHP extension, created using the Swig library. I've examined the Swig & PHP documentation carefully, but I can't seem to figure out how to parse a value out of php.ini the Swig way. To be clear: let's say I have a section in my php.ini like this:
[puppies]
puppies.cuteness=17
I want to expose a function in my PHP extension to retrieve this value. Something like:
int getCuteness(){
// access puppies.cuteness here, somehow!
}
I've found a variety of tutorials on writing PHP extensions that show how to do this, but I know that Swig wants to abstract a lot of this kind of thing away for me (and anyway, the usual methods don't seem to work in the Swig context).
So, how is this done? Is there a Swig way, or am I supposed to stick with the conventional technique? Have any of you done it? Thanks much!

PHP Libraries - what are they and how to create one

1) Is a PHP library (as in the GD Library) a compiled DLL (or other appropriate name if that is not used outside of Windows) written in a language such as C, compiled, and then "loaded" and made available to PHP code?
2) If this is the case, where can I find documentation on libraries which, among other things, includes calling, argument passing, and value return standards and protocals, and other information which I can use to get started writing PHP "libraries"? I am not looking for documentation on how to program in C or another language, I am looking only for specific, and detailed, information on creating "libraries" for PHP.
Bob
In the php world I believe it's called an extension and it's behave similar to a windows dll and maybe has something similar when you want to create one.
theserverpages.com/php/manual/en/zend.php. I think its part of the php documentation, the url www.php net may work also.

Use C or C++ libraries as PHP extension

Minutes back I read something like "it should be possible to link the C++ library as a php extension".
Is it really possible?
if yes, which is better, newly written extension or a linked library (efforts and performance wise).
Also can you guys point me to some How-to or related doc.
Yes it is, check these out:
http://devzone.zend.com/1435/wrapping-c-classes-in-a-php-extension/
How to start writing a PHP5 extension in C++
The following link will probably answer all your questions. Basically yes, it's possible to link C++ lib as PHP extension.

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