The Peachpie.Avalonia library will allow developers to easily create cross-platform applications for Windows, macOS, Linux, iOS, Android and web browsers in PHP in the .NET runtime.
Full .NET compatibility: Compiled programs run in the redesigned PeachPie runtime, which is fully compatible with the PHP runtime.
Two-way compatibility: the project allows hybrid applications, some of which are written in C#, and some in PHP. The parts will be fully compatible and will be able to interact seamlessly, all within the .NET framework.
Avalonia is a powerful framework that enables developers to create cross-platform application using .NET
Install .NET 8.0 SDK
Note
Run from a command line (.NET 8+
):
dotnet new install Peachpie.Avalonia.Templates
The templates should now be available in dotnet new list
:
Template Name Short Name Language Tags
----------------------------------- ------------------------- -------- -----------------------------------------
Avalonia PHP App php.avalonia.app PHP Desktop/PeachPie/Xaml/Avalonia/Windows/Linux/macOS
Avalonia PHP Library php.avalonia.lib PHP Library/PeachPie/Xaml/Avalonia/Windows/Linux/macOS
Avalonia PHP Window php.avalonia.window PHP Desktop/PeachPie/Xaml/Avalonia/Windows/Linux/macOS
PHP Console App php.console PHP Console/PeachPie
PHP Library php.lib PHP Library/PeachPie
To create a new barebones php application called MyApp
in its own subdirectory, run:
dotnet new php.avalonia.app -o MyApp
Go to the MyApp
derictory cd ./MyApp
and execute:
dotnet run
If you've done everything correctly, you should see a project template like this:
Unlike basic Avalonia UI components, Peachpie.Avalonia form components must start with «Ux». These components are child classes of Avalonia's base UI components, but with some changes to make development easier. (In the future I plan to avoid inheritance, but for now creating controls would look like this)
<?php
namespace Views {
use Avalonia\DevToolsExtensions;
use Avalonia\Layout\HorizontalAlignment;
use Avalonia\Layout\VerticalAlignment;
use Peachpie\Avalonia\Controls\UxButton;
use Peachpie\Avalonia\Controls\UxWindow;
use Php\Output\Logger;
use Views\Pages\HomePage;
class MainWindow extends UxWindow
{
public function __construct()
{
$this->InitializeComponent();
//Create a button
$button = new UxButton();
$button->Content = "I'm a super button, press me!";
$button->HorizontalAlignment = HorizontalAlignment::Center;
$button->VerticalAlignment = VerticalAlignment::Center;
$this->Content = $button;
Logger::Info("Hello Peachpie Avalonia!");
}
private function InitializeComponent(): void
{
Load();
if ( defined('DEBUG') ) {
Logger::Info("Debug Build!");
//DevTools press F12 Debug build
DevToolsExtensions::AttachDevTools($this);
}
}
}
}
use Avalonia\Interactivity\RoutedEventArgs;
$this->button1->on('Click', function(UxButton $sender, $RoutedEventArgs $e) {
Logger::Info("Hello Peachpie Avalonia!");
});
$handler = function(){
/////////////////////
};
$this->button1->on('Click', $handler);
use Avalonia\Interactivity\RoutedEventArgs;
public function BurgerButton_onClickArgs(UxToggleButton $sender, RoutedEventArgs $e): void
{
$this->Title = "BurgerButton_onClickArgs function";
}
public function BurgerButton_onClick($sender, $e): void
{
$this->Title = "BurgerButton_onClick function";
}
$this->button1->on('Click', [$this, 'BurgerButton_onClickArgs']) ;
$this->button1->on('Click', [$this, 'BurgerButton_onClickArgs'], 'BurgerButton_onClickArgs') ;
$this->button1->on('Click', [$this, 'BurgerButton_onClick'], 'BurgerButton_onClick') ;
Important
PeachPie runtime allows working with .NET/CLR event
class members in order to register and unregister callables. The following code depicts a sample C# class and a sample PHP program adding and removing an anonymous function (or any PHP callable) to it.
// add callable to the event handler:
$hook = $button1->Click->add(
function ($sender, $arg) {
Logger::Info("Click!");
}
);
// remove callable from the event handler
$hook->close();
<UxTextBlock Name="textblock1" Foreground="#ECF0F1" HorizontalAlignment="Center" Text="Hello PeachPie Avalonia!"/>
public UxTextBlock $textblock1;
$this->textblock1 = $this->FindByName("textblock1");
$this->Text = "Hello";