[ 
https://issues.apache.org/jira/browse/THRIFT-1926?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel
 ]

Xavier HAUSHERR updated THRIFT-1926:
------------------------------------

    Description: 
When I have added PHP namespace, one point was missing from my refactoring: the 
constants.

Actually, PHP constants are generated into $GLOBALS because PHP cannot generate 
constants for array or object with the function "define".

But there is some problems:
- Add vars in $GLOBALS is not a good pratice
- These vars are not constant because user can modify them
- Object constants instanciate a new object everytime the constant is call

So I have added a new PHP Class in library called TConstant. The goal of this 
class which is a Singleton, is to isolate Constant, and instanciate them 
on-demand (only one time) in static properties. There is no setter so constants 
cannot be edited and the class is abstract.

I have edited the PHP generator to create those constants given to the 
definition file. Every constant create a static property and an init method in 
a class called Constant which is final in the definition Namespace.

To get a constant, you just have to write:
\MyDefinitionNamepace\Constant::get('CONSTANT_NAME');

If the contant doesn't exists, it throws a Fatal error like it is done in PHP.

I have attached a diff patch made from the 
c613cd2d9bc01ee4eb43a3d40a8691c736f7a316 commit (Thrift-1846:Restore socket.h 
header to support builds with Android NDK). Because you aren't not in SVN 
anymore, I don't know how to send you this patch, I hope this one is good.

I hope you like my patch. 



  was:
When I have added PHP namespace, one point was missing from my refactoring: the 
constants.

Actually, PHP constants are generated into $GLOBALS because PHP cannot generate 
constants for array or object with the function "define".

But there is some problems:
- Add vars in $GLOBALS is not a good pratice
- These vars are not constant because user can modify them
- Object constants instanciate a new object everytime the constant is call

So I have added a new PHP Class in library called TConstant. The goal of this 
class which is a Singleton, is to isolate Constant, and instanciate them 
on-demand (only one time) in static properties. There is no setter so constants 
cannot be edited and the class is final.

I have edited the PHP generator to create those constants given to the 
definition file. Every constant create a static property and an init method in 
a class called Constant in the definition Namespace.

To get a constant, you just have to write:
\MyDefinitionNamepace\Constant::get('CONSTANT_NAME');

If the contant doesn't exists, it throws a Fatal error like it is done in PHP.

I have attached a diff patch made from the 
c613cd2d9bc01ee4eb43a3d40a8691c736f7a316 commit (Thrift-1846:Restore socket.h 
header to support builds with Android NDK). Because you aren't not in SVN 
anymore, I don't know how to send you this patch, I hope this one is good.

I hope you like my patch. 



    
> PHP Constant Generation Refactoring
> -----------------------------------
>
>                 Key: THRIFT-1926
>                 URL: https://issues.apache.org/jira/browse/THRIFT-1926
>             Project: Thrift
>          Issue Type: Improvement
>          Components: PHP - Compiler, PHP - Library
>            Reporter: Xavier HAUSHERR
>              Labels: constant, php
>             Fix For: 1.0
>
>         Attachments: 0001-PHP-Replace-constant-Generation.patch
>
>
> When I have added PHP namespace, one point was missing from my refactoring: 
> the constants.
> Actually, PHP constants are generated into $GLOBALS because PHP cannot 
> generate constants for array or object with the function "define".
> But there is some problems:
> - Add vars in $GLOBALS is not a good pratice
> - These vars are not constant because user can modify them
> - Object constants instanciate a new object everytime the constant is call
> So I have added a new PHP Class in library called TConstant. The goal of this 
> class which is a Singleton, is to isolate Constant, and instanciate them 
> on-demand (only one time) in static properties. There is no setter so 
> constants cannot be edited and the class is abstract.
> I have edited the PHP generator to create those constants given to the 
> definition file. Every constant create a static property and an init method 
> in a class called Constant which is final in the definition Namespace.
> To get a constant, you just have to write:
> \MyDefinitionNamepace\Constant::get('CONSTANT_NAME');
> If the contant doesn't exists, it throws a Fatal error like it is done in PHP.
> I have attached a diff patch made from the 
> c613cd2d9bc01ee4eb43a3d40a8691c736f7a316 commit (Thrift-1846:Restore socket.h 
> header to support builds with Android NDK). Because you aren't not in SVN 
> anymore, I don't know how to send you this patch, I hope this one is good.
> I hope you like my patch. 

--
This message is automatically generated by JIRA.
If you think it was sent incorrectly, please contact your JIRA administrators
For more information on JIRA, see: http://www.atlassian.com/software/jira

Reply via email to