c++ php and static library - php

I've created a library.a that contains a .cpp and .h files with a lot of classes, nested classes and methods. I would like to include this static library inside a php example and try to work with it. I would like to mention that I am new to php. I've tested my libray.a inside a test.cpp file and it works. How can I create a test.php and test my library.a?If it;s possible please send me some examples.
I would like to mention that i am working in ubuntu. g++ was my compiler.
Thx for advices! Appreciate!
EDIT:
I WOULD LIKE TO MENTION THE FACT THAT I DON'T WANT TO EXPOSE MY .CPP CODE. I JUST WANT TO USE MY .H AND .A FILES.

An .a file is not a self-executable library. It is static object code. It cannot run by itself.
PHP doesn't have loaders. It can't load a .a file, neither your very own operating system can.
An .a file needs to be accompanied by the appropriate headers (.h files).
If you want to use native code within PHP, you must use PHP's interfaces. See, just like anything built with C/C++, PHP has it's own definition of what a string (or most data types) look like.
In short, you have two options:
use PHP's headers and interface your code directly with PHP
use a library wrapper which connects your calls to PHP
make your library into an executable and call it with PHP*
*PHP has plenty IPC methods, so this is actually quite feasible.

Related

Creating a PHP extension extending another PHP extension?

I searched over the Internet several documentation about how to create PHP extensions, but unfortunately, there is nothing about linking to another extensions (and making a requirement for having that extension loaded prior to the new it is being created).
I guess I could simply #include necessary header files into my source code, but not sure about linking.
As an example, and to play with extension creation, the first I want to create is a solution I implemented to allow namespaces in memcached github but wanted to know how to use other extensions' code from my custom extension one for other usages as well.
I'm not sure how to reply to the thread with StormByte, but it sounds like you need to do some load balancing or caching, not extending PHP.
If you really want to do this at the code level, you could use exec() to call a Python script, which gets compiled into byte code automatically.

calling dll through php

I need to call a dll that returns a string using PHP.
What would be the best possible way to achieve this?
Build a PHP extension that wraps the DLL or create a wrapper (in any language) that can be accessed via shell with exec.
This is not possible using native PHP.
I would look into running an operating system level function to do this using exec(), like for example rundll.exe (for some kinds of DLLs).
If rundll can't do it (it has something to do with managed and unmanaged DLLs, I don't know what that means), the easiest way may be writing a wrapper application that imports the DLL, performs the necessary actions, and outputs the result.

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