In what exact use cases should a developer create a PHP extension instead of PHP library?
From what I found on google (very little is written on the subject).
PHP extensions are compiled libraries which enable specific functions to be used in your PHP code.
I also found that they are written in C.
Why should I compile a library?
What are the benefits over libraries?
If you could answer with an example, lets say zeroMQ or mySQL extension, that would be great.
Main point of writing extension in C instead of library in PHP is speed and/or memory-wise optimization. You can leverage C's means of optimizations, which are not available in PHP.
A good example here would be BC Math extension: most of the functionality could be written in pure PHP (although not all of it), but the main point here is the speed and memory-optimized computations.
Related
In regular php, there are multiple libraries for accessing mysql, such as mysql, mysqli, pdo and mysqlnd.
Which of these libraries are supported in php hiphop (https://github.com/facebook/hiphop-php/wiki/)?
Hiphip for PHP is not library/module specific. Built-in PHP modules are already written in C++ and compiled. You can write your own by hand. All that Hiphop for PHP does is automates that process for you. Since the built in modules are already compiled, Hiphop for PHP just ignores them. It has no bearing on what built-in libraries you can use in your code or what it works with.
The only place you may find issues with Hiphop for PHP is if you write closures or evals in your code. As of the last time I checked, Hiphop for PHP could not handle them. However, things may have changed since then.
EDIT:
Looks like interoperability with all built in functions was not fully implemented after all. Documentation and use of Hiphop is pretty minimal (as in you've already found it). You may just want to do some small tests to see if the libraries you want to use are supported. Do post the results back up here so others can benefit!
No php Hip-Hop does'nt support any of this libraries except PDO
I have also tried it but it did'nt work
For Further information please see below link:-
http://php.webtutor.pl/en/2011/05/10/hiphop-for-php-unimplemented-functions-and-features/
I have a PHP class I want to convert to a PHP extension. I checked some tutorials (tuxradar's writing extensions, php.net's extending php, and zend's extension writing) and it's a bit complicated.
I found the article "How to write PHP extensions" (ed note: site is defunct) and I wanted to know if it is possible to use this to make it grab a PHP class from a certain path (say /home/website1/public_html/api/class.php), execute it and return the class instance.
This way it will be usable in other websites that are hosted on the same server – each can simply call the function and it will obtain its own instance.
Is that possible?
The question as I understand it now is, The user has a PHP class that they would like to share with multiple people, but does not want to share the source code.
There are many solutions to this, they generally invovle turning the PHP code into some kind of byte code, and using a PHP extension to run the byte code. I've never used any of these solutions, but I'm aware of the following:
phc is an open source compiler for PHP
Zend Guard
HipHop for PHP - I'm unsure about this, but Facebook recently released it so it might be worth a look.
I'm sure there are others. Just Google for PHP Compiler, or PHP Accelerator.
In one sentence: I don't believe so, I think its a lot more work than that.
No, there is not tool that can do that.
Anyway, what you want call be easily accomplished with auto_prepend_file. Just make that ini directive point to a PHP file that has the class definition, and then it will be available to all the applications.
If you don't want the users to be able to use the source, you can use one the several zend extensions that allow you to pre-compile the file and use it in that form.
You can extend underlying C library functions into PHP space by writing PHP extensions. However, i think in your case you don't need to write one.
I am aware that this is an old question (being from 2012) however the answer has changed and there is now a tool that can do this. Jim Thunderbirds PHP-to-C Extension toolset provides the means to take a simple class in one file all the way up to a complicated multi file multi-level namespaced framework and convert it to a C-extension that can then be installed into your PHP server.
While in many use cases doing so is not needed as the ordinary PHP code will work just as good in some cases significant performance improvements can be experienced. The information page shows that an ordinary class (deliberately designed to take a long time) took 16.802139997482 seconds as plain vanilla PHP, and 3.9628620147705 as a PHP extension built using the tool.
As an added advantage the tool also provides an additional feature. The ability to combine PHP code (to be converted to C) and native C code within the same extension which can produce even greater performance enhancements. The same example used above only tool 0.14397192001343 seconds when much of the intensive code was moved to a bubble sort C code and simply calling it from within the PHP code.
As a side note functionally to the end developers using the code using the extension is very much similar to having the files manually included in the PHP file being developed except it doesn't have to be specifically included as it is done through the PHP extensions component.
(Disclaimer: I am not affiliated with this developer but am glad to have come across it as it is thus far working for converting some of my intensive classes into PHP extensions without needing to know C).
I am wondering if there are any languages that extend PHP into something ahem "better"?
They don't have to necessarily be able to interact with PHP, but it is certainly a benefit if they can (e.g. call PHP functions or even be called from PHP).
One example:
The LOL Code PHP Parser: http://www.tetraboy.com/lolcode/
There's Facebook Hip Hop. It takes PHP, compiles it down to C++, and then compiles that into a big fat binary with a webserver included. Then you just deploy that. It ends up being a big file - weighing in at 1 GB - but it reduces server load by upwards of 50%.
We have some discussion on php|architect - http://www.phparch.com/?s=hiphop
There is Haxe, that compiles into PHP (among other things): Haxe.org
Here's an article from CodingHorror about Wasabi, a language which apparently compiles down to PHP among other strange things:
http://www.codinghorror.com/blog/2006/09/has-joel-spolsky-jumped-the-shark.html
If you mean extending PHP's capabilities, I assume the plentiful modules that exist for it already do that...
FogBugz is written in a custom in-house extension of VBScript that they can compile to either VBScript or PHP (for Windows and Unix servers, respectively). Their compiler is written in C#.
http://www.joelonsoftware.com/items/2006/09/01b.html is a good read.
My PHP app has a number-crunching part that is just to slow for PHP, so I was thinking of building a custom C extension, but it is impossible to find any good reference to start with :(
Is there a guide on how to do something like this?
The best resource, although outdated in several aspects (it only covers PHP until version 5.1) is Extending and Embedding PHP by Sara Golemon. Even more outdated is the PHP documentation. On the other hand, the content on the PHP wiki is very up-to-date, but also quite incomplete and not very oriented for beginners. See also these articles, part V of Advanced PHP Programming by George Schlossnagle, chapter 14 of Programming PHP by Rasmus Lerdorf and Kevin Tatroe and, specially, these slides.
Finally, the most authoritative source you'll find is the source code of the extensions bundled with PHP.
This might not be an answer but more like a suggestion, There are tools out there to compile your php to an executable, which you could just then use as an extension. This Would uniform your code a bit and unify your project. I have tried something like this a while ago. The compiled php acts no differently than the compiled c would.
Another option would be a command line tool written in C, that you start from PHP and communicate with over stdin/stdout. In many cases this is much easier to write and to deploy, but it ultimately depends on your use case.
I'm trying to accomplish a task and turns out that the code I need is packaged as a PHP extension, which according to what I've been told means I have to have root access to install it (I'm on shared hosting so that's a bit of a problem.
I'll solve this problem later, but for now I'm trying to understand the difference between an extension, a library, and a class. Is it more of a packaging thing that could be overridden and repackaged a different way, or is there a valid architectural reasoning behind it?
Also when releasing your own code, what makes you decide to release as library vs. class vs. extension? or do you go with whichever sounds better?
thanks in advance.
P.S. If you must know which extension I'm talking about, it's Libpuzzle, but that's really beside the point, my question is more general.
An extension is a pice of code programmed in C which will be included into the PHP core when PHP starts. Normally you have some more native functions available after including a extension. For example a zip functionality.
A class is a abstract pice of PHP code which solves common tasks. For example sending emails. You can find some common classes at pear.php.net.
A library is a collection of PHP classes wich solve more generic tasks for example buliding HTML forms AND sending emails. The Zend Framework is a framework which consists of many, many PHP classes.
Normally extension features can be programmed in PHP. For example the PEAR::Compat class. Often you will find the functionality you need as a PHP class available. I'm sure the stackoverflow readers will supply you with ideas where to find a specific PHP class.
Extensions are low-level. Usually written in C/C++, and compiled into native-code shared libraries, they interact with the Zend Engine directly. It has pros and cons, main advantages being the speed and more control; and main disadvantages - they are harder to install, and require compilation (and that requires a compiler and PHP headers); it's not true they require root access though - you only need ability to use custom php.ini (or dl() function, but I see they deprecated it for some reason).
Libraries/classes are high-level and interpreted. If you don't know if you need to write extension, then you probably don't. About what classes are - read about OOP. A library is a reusable collection of code (most commonly in form of functions/classes).
Some libraries (including libpuzzle) also include a command-line tool. So if you're unable to use the PHP library due to your shared hosting environment, maybe you can compile the command-line tool. Then you can run it from PHP using something like exec. It will be slower and require more memory than a library, but it might get the job done. Of course, many hosts also have restrictions on commands like exec, so this might not work either.