What’s New in PeachPie 0.9.18 – Attributes and Generics¶
We just released PeachPie 0.9.18, which targets some syntax extensions, so that PeachPie can provide more and much needed CLR interoperability features. Extending the syntax does not affect any existing PHP code and only provides developers with more options.
PeachPie 0.9.18¶
The latest minor version update of PeachPie, the PHP compiler and runtime on top of .NET & .NET Core, includes some huge advances on our quest to provide the most seamless interoperability experience between PHP and .NET. In this article we’ll take a look at what’s new in the most recent version of PeachPie, one of the final minor versions before the next major, which will be 1.0.
Project file reference
Take a look at our documentation on how to run the latest version of PeachPie at https://docs.peachpie.io/php/msbuild.
Let’s see what you can take advantage of starting with PeachPie 0.9.18.
.NET Generics¶
It is now possible to call generic methods and instantiate generic classes. However, it is not possible to declare a generic class in PHP nor a generic function. All the type arguments must be specified and resolved during compile time and it is not possible to use indirect types (types specified via a variable). See the following sample:
<?php
// instantiate a generic .NET class in PHP
$x = new System\Collections\Generic\Dictionary<int, string>;
// access a generic .NET class and its static member
$y = System\Collections\Generic\Comparer<int>::$Default;
// Call a generic .NET method in PHP
$z1 = System\Activator::CreateInstance<object>();
$z2 = $urhoNode->CreateComponent<UrhoLight>();
Notice the PHP syntax now supports the pattern < >
for generic type arguments. This can be inserted after a type name or function name. For example the last line of the code makes use of a .NET library called “Urho” (https://nuget.org/packages/urhosharp), which allows us to create 3D apps for Android, iOS and other devices. Since it heavily depends on a usage of .NET generics, it is only possible to use the library since this release without some major workarounds, as we did in this article.
Additionally, until now the compiler did not recognize generic types and methods and when attempting to use them, invalid IL was generated. This has now been fixed. See the full documentation on .NET generics for additional details. .
.NET custom attributes¶
Sometimes it is necessary to annotate a class or method with an attribute. This is now also possible since this release.
<?php
#[System\Obsolete("An attribute parameter")]
class X {
#[Amazon\Lambda\Core\Lambda\Serializer(
typeof(Amazon\Lambda\Serialization\Json\JsonSerializer)
)]
function foo() {
// …
}
}
See the example above. Attributes can prefix a class declaration or a function declaration. It starts with the type name, and if the type name is not suffixed with Attribute
, the compiler automatically assumes the suffix (just like in C#). After that, the parameters follow in the brackets. The syntax also understand the keyword typeof
. Optionally, properties in the form of PropertyName = Value
can also follow. See the full documentation on custom attributes to learn more.
What's next¶
We don’t foresee too many more minor versions before we release PeachPie 1.0. Until then, we will continue working on stabilizing the platform, i.e. fixing issues people report, implementing missing functionalities, and also doing some basic performance optimizations. Before releasing version 1.0, we’d also like to be able to run one or two larger useful applications on PeachPie.