php5 -> 4的代码转换工具
在zend的论坛上看到php5->php4的代码转换工具,如果你要将php5代码放到低版本的服务器上使用,这个东东可能就可以派上用场了:)
支持下列语法结构:
- abstract classes and methods- interface, implements
- new constructor syntax __construct
- final classes and method
- visibility modifiers public, protected, private
- static attributes and methods
- class constants
- object clonning
- operator instanceof
- self::
- automatic passing objects as references
- type hinting
- support for Doc Comments
- any partially supports exceptions
测试php5代码:
PHP:
-
<?php
-
-
class MyClass implements iTemplate
-
{
-
protected $b = 'hello world';
-
private static $instance;
-
-
/**
-
* Singleton demo
-
*/
-
public static function singleton()
-
{
-
if (!isset(self::$instance)) {
-
$c = __CLASS__;
-
self::$instance = new $c;
-
}
-
return self::$instance;
-
}
-
}
-
-
$test = MyClass::singleton();
-
-
?>
转换之后的php4代码:
PHP:
-
<?php
-
-
$GLOBALS['MyClass::$instance'] = NULL; /* class private static property */
-
class MyClass /* implements iTemplate */
-
{
-
var $b = 'hello world'; /* protected */
-
/**
-
* Singleton demo
-
*/
-
function singleton() /* static */
-
{
-
if (!isset($GLOBALS['MyClass::$instance'])) {
-
$c = __CLASS__;
-
$GLOBALS['MyClass::$instance']= new $c;
-
}
-
return $GLOBALS['MyClass::$instance'];
-
}
-
}
-
-
$test = MyClass::singleton();
-
-
?>
可参考文档:
http://www.dgx.cz/trine/item/how-to-emulate-php5-object-model-in-php4
作者: Volcano 发表于December 9, 2005 at 10:02 pm
爆米花 于 2005-12-10 @ 16:15:53 留言 :
呵呵,好一阵没看PHP代码了! 都有些陌生了
在php5中,使用singleton模式,类实例是保存在内存中,还是只在本次请求中有效!?
但从转换后的php4代码来看,类静态变量似乎也只对应于单次请求
volcano 于 2005-12-10 @ 20:13:58 留言 :
当然只在本次请求中有效,php好像没有持久化的对象..
爆米花 于 2005-12-11 @ 18:22:06 留言 :
serialize() / unserialize() 不是做持久化工作的吗? 只不过,不能放到内存中罢了!
volcano 于 2005-12-11 @ 19:35:33 留言 :
unserialize()一个对象的时候,会重建(new)这个对象,只不过它的属性还被保留罢了.如此看来,跟新建一个对象的开销是差不了多少的,所以说,这也算不得持久化
Koie 于 2007-05-16 @ 20:38:01 留言 :
我正有這個問題
我想將PHP5 的 CLASS 轉為 PHP4的
可是剛才試試, 轉換不了, 輸入的跟輸出的一樣
請問還有其他方法嗎 ?
volcano 于 2007-05-16 @ 20:48:47 留言 :
可以把你的php5 CLASS贴一些片段上来么