windows+apache+php4+php5

七 8th, 2009

由于客户主机环境不同,经常要写php4的代码.同时又要正常运行在PHP5环境下.
所以一直是自己开发用的电脑安装PHP5而写PHP4的代码. 偶尔会出现一些问题.例如引用问题:

 class test1{
    var $var = 0  ;
	function test1(){
        $this->var += 1;
	}
 
    function set($n){
	   $this->var +=$n;
	}
 
	function get(){
	  return $this->var ;
	}
 
}
 
class test2{
	function getInstance(){
        static $instance = array();
		if(!isset($instance[0])){
		    $instance[0] = & new test1();
		}
		return $instance[0];
	}
}
 
$t1 = test2 :: getInstance();
$t1->set(3);
echo $t1->get();
echo "<br />";
$t2 = test2 :: getInstance();
echo $t2->get();

上边的代码在PHP4下输出:
4
1
而在PHP5下输出:
4
4
而下面的代码结果会相同 4 4 :

/**
* 注意 & 的使用
*/
 
class test1{
    var $var = 0  ;
 
	function test1(){
        $this->var += 1;
	}
 
    function set($n){
	   $this->var +=$n;
	}
 
	function get(){
	  return $this->var ;
	}
 
}
 
class test2{
	function & getInstance(){
        static $instance = array();
		if(!isset($instance[0])){
		    $instance[0] = & new test1();
		}
		return $instance[0];
	}
}
 
$t1 =& test2 :: getInstance();
$t1->set(3);
echo $t1->get();
echo "<br />";
$t2 =& test2 :: getInstance();
echo $t2->get();

还有一些PHP4 和 PHP5 的不兼容问题也是很让人烦 如时区问题
所以得让自己写的代码是否能在PHP4和PHP5下都能正常运行
以前是用下边的方法:
C:/php4
C:/php5
C:/apache2
在httpd.conf中换PHP4和PHP5再重启apache来实现
今天上网找到个更好的方法
参考 : http://www.wangchao.net.cn/bbsdetail_547736.html

1.在httpd.conf中加入代码

  <IfDefine !php5>
   #我的电脑设定的IP地址
    Listen 192.168.1.99:80
    LoadModule php4_module "c:/php4/sapi/php4apache2.dll"
    PHPIniDir "C:/php4"
&lt/IfDefine>

<IfDefine php5>
    Listen 127.0.0.1:80
    LoadModule php5_module "C:/php5/php5apache2.dll"
    PHPIniDir   "C:/php5"

</IfDefine>

2. 开始-运行-CMD-

  cd c:/apache/bin/
  apache -k install -D php5 -n Apache2-PHP5
  net start Apache2-PHP5

现在使用 http://192.168.1.99 会是PHP4
而在使用 http://127.0.0.1 (或localhost) 会是php5
就这样解决了我的问题.可以同时使用php4和php5来测试代码了^_^

标签:
  1. derekpm
    七 13th, 200914:41

    Rather interesting. Has few times re-read for this purpose to remember. Thanks for interesting article. Waiting for trackback