PHP code version dependency

本秂侑毒 提交于 2019-12-06 04:27:41

Check out the PHPCompatibility project : https://github.com/wimg/PHPCompatibility This tests your code for compatibility with 5.2, 5.3, 5.4 and 5.5(alpha2 at the time of writing)

Don't know if there is a way to check in php wich version is needed, but you could use "function_exists" to see if your version of php has that function.

http://php.net/manual/en/function.function-exists.php

<?php
if (function_exists('hex2bin')) {
    echo "hex2bin function is available.<br />\n";
} else {
    echo "hex2bin function is not available.<br />\n";
}
?>

It's up to the programmer to state the dependencies of his or her library / project.

Many projects now use Composer as a dependency manager, but for legacy code you can't do much but running it and swearing against errors.

Good practice could be to have fallbacks. Just like mentioned in this function, there are alternatives.

if ( ! function_exists('hex2bin')) {
    function hex2bin($hexstr)
    {
        $n = strlen($hexstr);
        $sbin="";  
        $i=0;
        while($i<$n)
        {      
            $a =substr($hexstr,$i,2);          
            $c = pack("H*",$a);
            if ($i==0){$sbin=$c;}
            else {$sbin.=$c;}
            $i+=2;
        }
        return $sbin;
    }
}

Code is based on Johnson's reply.

Setting essential function which does that same as new ones in case they don't exist lets you go about your business without having to fear incompability. Just hide these where you don't have any reason to edit them and they won't bother you either.

You can create a manual checking ,then using 'patch'.To define what is missing.

Using phpversion();

 if (strnatcmp(phpversion(),'5.2.10') >= 0) 
 { 
       //loading patch

 } 
 else 
 { 
     //loading patch
      function abc(){
        //
      }
 } 
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!