Strategy for developing namespaced and non-namespaced versions of same PHP code

前端 未结 9 2062
时光取名叫无心
时光取名叫无心 2020-12-23 13:43

I\'m maintaining library written for PHP 5.2 and I\'d like to create PHP 5.3-namespaced version of it. However, I\'d also keep non-namespaced version up to date until PHP 5.

9条回答
  •  自闭症患者
    2020-12-23 14:04

    This is a followup to my previous answer:

    The namespace simulation code got quite stable. I already can get symfony2 to work (some problems still, but basically). Though there is still some stuff missing like variable namespace resolution for all cases apart from new $class.

    Now I wrote a script which will iterate recursively through a directory and process all files: http://github.com/nikic/prephp/blob/master/prephp/namespacePortR.php


    Usage Instructions

    Requirements for your code to work

    Your classnames mustn't contain the _ character. If they do, classnames could get ambiguous while converting.

    Your code mustn't redeclare any global functions or constants within a namespace. Thus it is ensured that all your code may be resolved at compile-time.

    Basically these are the only restrictions to your code. Though I should note that in a default configuration the namespacePortR will not resolve things like $className = 'Some\\NS\\Class'; new $className, because it would require inserting additional code. It's better that this is patched up later (either manually or using an automated patching system.)

    Configuration

    As we have made the assumption that no global function or constant is redeclared in a namespace you must set the assumeGlobal class constant in the namespace listener. In the same file set the SEPARATOR constant to _.

    In the namespacePortR change the configuration block to satisfy your needs.


    PS: The script may be provided a ?skip=int option. This tells it to skip the first int files. You should not need it, if you have set the override mode to intelligent.

提交回复
热议问题