I'm writing a Wordpress plugin, so it's going to be used by different users and different PHP versions. The problem is that I found that some of the functions (like json_encode) are available in PHP 5.3 and not in PHP 5.2 or less. This creates a big issue, as most users don't have the latest version.
I want now, after getting the plugin 99% done, to do the following
Test my code with some kind of an app. where I can put the minimum PHP version. That App. or program would find out functions like json_encode. Not sure if that is possible, but would probably solve most of my problem.
Is it possible to get the native code of the PHP functions in PHP. I'm not sure if they are written in PHP or not, if so where can I get them. If not, what's the best option to find replacement for these functions. Certainly, I don't want to be re-coding them from scratch
What's the best methodology to implement the functions. I found some developers that check for the PHP version, while others check if the functions exist. Which one is best and why?
Would love also to read about your deployment strategies and how you dealt with that particular problem.
2.Nope. They're provided by compiled extensions written in C. For json_encode (which BTW is available since 5.2 not 5.3) you can use Zend_Json as an alternative
3.Checking if function exist is the best if you want to be 100% sure. After all, one can be running a self compiled version of PHP with not all core functions available. Check PHP version number, to know if features like namespaces, exception chaining etc are available.
You can use PEAR's
PHP_Compat to provide missing functionality for older versions of PHP and
PHP_CompatInfo to find out the minimum version and the extensions required for a piece of code to run
If you want to provide your own userland implementations of functions or classes, you are good advised to provide them wrapped into function_exists or class_existsblocks, so they dont interfere with PHP versions providing those methods.
I don't really know how Wordpress plugins work but except if you really want your to be able to run on old PHP version, you'll need to check for available function and provide an alternative, which can lead you code to be messy.
If you can, and want to educate your users, you can simply version_compare() function to compare version against a well tested and fully functionnal PHP version and throw a educationnal and explicative message to your end users.
if (version_compare(PHP_VERSION, '5.3.0') <= 0) {
echo 'You need to run PHP 5.3.0 to use this plugin';
}
Re 1.) there may be such an application, but there also may be not. What you usually want to do is to keep close tabs on each function you use in the manual, which states what PHP version(s) are needed.
Re 2.) if a function is PHP 5.3 or 5.2 only, you will usually find a replacement suggestion in the User Contributed Notes or on Stack Overflow. (Be careful what you use though, a lot of the code in the UCN is bad - but in that case, there is usually at least one comment saying so.)
Re. 3.) it probably doesn't matter, but checking for whether a function exists is surely the safest way.
Related
I've been tasked to migrate an old PHP4.3 site (using CVS version control) to a more modern 5.2 install (lower version number for compatibility issues) on a shared GoDaddy server. I've gotten through most of the issues, but this particular one is a tough nut.
Inside the administration view, there is an export to excel function which uses the Spreadsheet_Excel_Writer (0.9.3) PEAR package. However, this line keeps throwing a fatal error:
aggregate( $sheet, 'Spreadsheet_Excel_Writer_Sheet_Patch' );
where $sheet is defined by
$sheet =& $xls->addWorksheet( 'Customer Data' );
My issue is that aggregate no longer exists in PHP5, and I cannot install runkit as I do not have root permission to execute the pecl install command on the server.
I'd rather not have to rewrite the existing functionality, so is there a drop-in replacement for aggregate that I can install without requiring root permissions?
With regard to the Excel library you're using, you will need to upgrade that to a version that supports PHP5, or else migrate to an alternative library. There are several Excel libraries available which do support recent PHP versions, so you should probably investigate those.
I won't recommend any specific library now, as I haven't worked with any of them for a little while and so my recommendation may not be the best. And in any case, your needs may be different to mine. Instead, simply googling for 'php excel' will give you plenty of results to help you find a library to suit you. You will obviously need to rewrite your code where it uses the old library so make it work with whatever new library you choose, but that will be infinitely easier than trying to make the old library itself work. (also, newer libraries will support newer excel versions, which is probably quite important; things do move on)
I certainly wouldn't recommend trying to keep using the existing library; it's clearly written for PHP4, and would likely be a very difficult task to bring it up to date.
You mention runkit: that wouldn't likely be a good solution here even if you were able to use it. For something closer to what the aggregate() function does, you might want to investigate the Reflection class, which allows some similar class- and object-level trickery, but in general well-written code shouldn't need that kind of thing anyway (it may have done in PHP4 days, but much less so today).
Another alternative to aggregate() might be the Traits feature in PHP 5.4 and up. This can give some of the multi-inheritance functionality that aggregate() was sometimes used to simulate. Obviously, you'd need to upgrade to 5.4 for this though, and the actual code you'd write would be quite different.
Is there a good way to test if a PHP project works well with a new version of PHP?
Lets say we have a project developed under PHP 5.0 and want to run it now with PHP 5.4. The project have no unit tests or something like that.
Just run it with PHP 5.4 and click around to see if there are errors is not save enough.
Run your tests. If you don't have tests, write some now under PHP 5.0. Then run them under 5.4. If they break, then you've found something that needs to be fixed. Having a suite of test scripts is good practice anyway so if you haven't got any, this is a good opportunity to start writing them. Look up phpUnit, which is the most common PHP tool for writing unit tests.
For creating a test suite on an existing project, I recommend using a tool like Selenium or Sahi which can record a browser session. Turn on the recording and start testing as normal. Voila: One repeatable test. You're going to have to do this kind of testing anyway, so you may as well record them. Granted, those are functional tests, rather than unit tests, but they are tests all the same, and if you can cover enough of your functionality with them then you'll have a fairly comprehensive demonstration that the system is working.
Syntax check: Use PHP's command-line -l option in a batch job to run a syntax check on all your files. This will prove that everything parses successfully.
That will eliminate the obvious problems.
Use a decent IDE to develop your code in. IDEs like Netbeans will highlight syntax issues and warning for you and underline the relevant code. This makes finding bugs much much easier.
If you're still developing in Notepad, you're missing out on a whole world of good stuff.
If you're using ereg() or related functions, they need to change to preg_match() etc. You can get away with still using mysql_query() for DB access, since that's only deprecated in 5.5, but if you're using it you may as well consider this to be a good time to make that change too.
Look up the Migration Guides provided by PHP. These give full details of all the code-breaking changes between PHP versions. In particular, pay attention to the deprecated features.
The most significant version for this was 5.3: A lot of old code was broken by the features which were deprected in 5.3. These were features like magic_quotes and register_globals; they had been considered bad practice for a very long time before that, but it took them until 5.3 to actually deprecate them. If you're using them, this will be the biggest problem you'll have to face.
Tools like PHPMD and PHPCodeSniffer, PHP Lint may help to analyse your code. They aren't really designed for version compatiblity checking, but may help you find issues.
I would like to override the _() PHP function so that it supports one or more arguments. Is it possible to do that using PHP code?
No, but with PHP version >= 5.3.0 you could use namespacing though.
It is possible, using the Runkit extension.
However, it's generally not considered a good idea, except for use with things like unit testing, where you may want to isolate some of your functionality.
For general use, you shouldn't be overriding built-in functions because it makes your code harder to maintain, and opens you up to some very hard to debug issues.
Also, the Runkit extension is marked as 'experimental', which means it really shouldn't be used in a production system.
You could try the runkit extension but it's considered a bad practice in production environments. See also Redefining PHP function?
Really don't do this! Even if you are the only developer on this project and know that your project won't be successful, one can never know how long your code will be in use (often much longer than you would think). Should another developer have to dive into your code, he will have a very hard time, because he/she cannot rely on PHP itself.
A better way would be to write your own methods/functions, which then call the PHP function you want to overwrite. This way a developer immediately can see, that this is not the standard PHP function, and even if PHP will allow other parameters in future versions, you will have a clean solution.
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 developing a php application which my customers will download and install on their own servers. I know the base requirements for my application (like min. php version) but is there a way to generate a list of requirements that needed to run my application on windows or unix systems?
Thanks.
You mean, generate a list of requirements based on an analysis of your source code?
While in theory, that might be possible, I don't think such a solution exists. I think there is no way than analyzing your code by hand, with the PHP manual very close by.
Do you use GD? Then you need PHP with the GD module. Do you need to create GIF images with GD? Then you need GD, but not between versions 1.6 and (I think) 1.8. Do you use PDO? Then you need PHP > 5.1.0. And so on and so on.
In short, I'm afraid think this is going to be a manual process. Manual also as in "PHP manual" - the User Contributed Notes to each function and method are a gem, and any common cross-platform problems are usually noted there somewhere.
While you can trust that PHP x.y.z has a defined set of functions and behaviour, be sure to test well before you declare something suitable to run on a different server. IIS's support of PHP is way better now, I'm told, but the last time a ported a big PHP application over to IIS, it took me three days to work around all the mysterious bugs.
Just be aware of what you are using. For example, you should clearly communicate if you need something like .. a special database binding ( other then mysql ), xml libraries etc.., or even better, create an installer that is bundled with your software that checks that kind of stuff.
Other than that, there should be no problems concerning different servers ( apache / iis / fastcgi.. ). So to answer your question: you have to generate that list all by yourself.
As others have said, you'll need to manually keep track of special libraries and functions you're using. If you need PHP4 compatibility then you won't be able to use the built-in XML libraries for example. You can also check the list of functions added to PHP 5.
One thing I would recommend is installing WampServer if you have access to a Windows machine. Aside from being good for local development, you can download modules for most Apache/PHP/MySQL versions and test combinations.