peachpiecompiler / peachpie

PeachPie - the PHP compiler and runtime for .NET and .NET Core
https://www.peachpie.io
Apache License 2.0
2.31k stars 201 forks source link

strange compiler behavior when modifier is "protected" #1112

Closed N0zzy closed 1 year ago

N0zzy commented 1 year ago

Greetings. I observe strange behavior of the pie and reflection. when creating a class property using a php script with modifiers protected static - pie compiles in static as public and reflection points to this fact. is this a bug?

1 2

jakubmisek commented 1 year ago

Hi,

This is by purpose; PHP static and .NET static behave differently. In PHP, a static property is static within the current request only. In .NET, static would be across all the requests.

To overcome this difference, PeachPie puts static properties into a nested class _statics, and creates a single instance for each request if needed.

Details at https://docs.peachpie.io/api/assembly/compiled-class/#additional-class-members

If it would be static, its value would be shared by all threads and all requests which would break the PHP behavior.

--

If you need a property, that is truly static (in a .NET manner), you can annotate it with Doc Comment @appstatic:

/*
 * field
 * @appstatic
 */
protected static $dTest;

This will create a property which value is shared across all requests - you can use it as optimization for example.