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:
  1. <?php
  2.  
  3. class MyClass implements iTemplate
  4. {
  5.    protected $b = 'hello world';
  6.    private static $instance;
  7.  
  8.   /**
  9.    * Singleton demo
  10.    */
  11.    public static function singleton()
  12.    {
  13.        if (!isset(self::$instance)) {
  14.            $c = __CLASS__;
  15.            self::$instance = new $c;
  16.        }
  17.        return self::$instance;
  18.    }
  19. }
  20.  
  21. $test = MyClass::singleton();
  22.  
  23. ?>

转换之后的php4代码:

PHP:
  1. <?php
  2.  
  3. $GLOBALS['MyClass::$instance'] = NULL; /* class private static property */
  4. class MyClass /* implements iTemplate */
  5. {
  6.    var $b = 'hello world'; /* protected */
  7.   /**
  8.    * Singleton demo
  9.    */
  10.    function singleton() /* static */
  11.    {
  12.        if (!isset($GLOBALS['MyClass::$instance'])) {
  13.            $c = __CLASS__;
  14.            $GLOBALS['MyClass::$instance']= new $c;
  15.        }
  16.        return $GLOBALS['MyClass::$instance'];
  17.    }
  18. }
  19.  
  20. $test = MyClass::singleton();
  21.  
  22. ?>

可参考文档:
http://www.dgx.cz/trine/item/how-to-emulate-php5-object-model-in-php4

作者: Volcano 发表于December 9, 2005 at 10:02 pm

版权信息: 可以任意转载, 转载时请务必以超链接形式标明文章原始出处作者信息及此声明

Tags:

6 条评论 »

  1. 爆米花 于 2005-12-10 @ 16:15:53 留言

    呵呵,好一阵没看PHP代码了! 都有些陌生了
    在php5中,使用singleton模式,类实例是保存在内存中,还是只在本次请求中有效!?
    但从转换后的php4代码来看,类静态变量似乎也只对应于单次请求

  2. volcano 于 2005-12-10 @ 20:13:58 留言

    当然只在本次请求中有效,php好像没有持久化的对象..

  3. 爆米花 于 2005-12-11 @ 18:22:06 留言

    serialize() / unserialize() 不是做持久化工作的吗? 只不过,不能放到内存中罢了!

  4. volcano 于 2005-12-11 @ 19:35:33 留言

    unserialize()一个对象的时候,会重建(new)这个对象,只不过它的属性还被保留罢了.如此看来,跟新建一个对象的开销是差不了多少的,所以说,这也算不得持久化

  5. Koie 于 2007-05-16 @ 20:38:01 留言

    我正有這個問題
    我想將PHP5 的 CLASS 轉為 PHP4的

    可是剛才試試, 轉換不了, 輸入的跟輸出的一樣

    請問還有其他方法嗎 ?

  6. volcano 于 2007-05-16 @ 20:48:47 留言

    可以把你的php5 CLASS贴一些片段上来么

RSS 为此帖反馈评论 · 反向跟踪 网站

留条评论