You are here: Re: Checking PHP version for compatibility? « PHP Programming Language « IT news, forums, messages
Re: Checking PHP version for compatibility?

Posted by Colin Fine on 09/24/06 16:28

Jerry Stuckle wrote:
> Colin Fine wrote:
>> Mateusz Markowski wrote:
>>
>>> rustysmith@bellsouth.net napisal(a):
>>>
>>>> Anyone know how to check the PHP version that a current file is
>>>> running on? This would be helpful in the instance where you are trying
>>>> to make a package that can be distributed publicly for use in a variety
>>>> of configurations. This code should(?) work:
>>>>
>>>> $ver=PHP_VERSION;
>>>> if(ereg('^[1-4]',$ver))
>>>> $post_ids=array_flip(&$post_ids); // Old syntax
>>>> else
>>>> $post_ids=array_flip($post_ids); // PHP 5+ syntax
>>>>
>>>
>>> For version comapring you should write:
>>>
>>> if (version_compare(phpversion(), '4.0.0', '>='))
>>> //version above 4.0.0
>>> else
>>> //version below 4.0.0
>>>
>>> But it won't work in a way you want. For example:
>>> <?php
>>>
>>> if (version_compare(phpversion(), '5.0.0', '>=')){
>>> //PHP5
>>> class Foo{
>>> private $foobar;
>>> public function __construct(){
>>> echo 'Foo';
>>> }
>>> }
>>> }else{
>>> //PHP5
>>> class Foo{
>>> var $foobar;
>>> function Foo(){
>>> echo 'Foo';
>>> }
>>> }
>>> }
>>> ?>
>>> The PHP4's parser will give you a parse error.
>>> If you want to be compatibile with both versions, just write in PHP4.
>>>
>> But then you have to turn NOTICES off (not necessarily in this case,
>> but in some incompatibilities).
>> I've ended up taking my own copy of PEAR::Date and editing it to
>> declare all the static functions as static in order to use it in PHP5
>> without notices.
>> I can't believe this is the right way to deal with the problem, but I
>> don't know what else to do.
>>
>> Colin
>
> Maybe put the level-specific code in two different files and include the
> appropriate file based on the PHP version?
>
Well I guess that might do for the original poster, who wants to provide
a package that will work with either version; though it seems a horribly
messy way of handling an obvious problem.

For my purposes, all I want to do is use an existing PEAR class in PHP5.
AFAIK this class is not available in a PHP5 version, so the best I have
thought of doing, as I say, is taking my own copy and editing it.

Now, there may be some PEAR packages which are so dependent on the way
PHP4 works that they will require a rewrite for PHP5: but the package I
am using (Date) seems to require only that a number of functions be
declared 'static'.
Given this, is seems a pity - and surprising - that PHP apparently
offers no way that the writer could make the package acceptable to both
PHP4 and 5. Or is there such a way, that I'm missing?

Colin

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация