PHP code example of developersharif / php-gui

1. Go to this page and download the library: Download developersharif/php-gui library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

developersharif / php-gui example snippets



PhpGui\Application;
use PhpGui\Widget\Window;
use PhpGui\Widget\Label;
use PhpGui\Widget\Button;

$app    = new Application();
$window = new Window(['title' => 'Hello PHP GUI', 'width' => 400, 'height' => 250]);

$label = new Label($window->getId(), ['text' => 'Hello, World!']);
$label->pack(['pady' => 20]);

$button = new Button($window->getId(), [
    'text'    => 'Click Me',
    'command' => fn() => $label->setText('You clicked it!'),
]);
$button->pack();

$app->run();

// Pack — flow layout (simplest)
$widget->pack(['side' => 'top', 'pady' => 10, 'fill' => 'x']);

// Grid — row/column table
$label->grid(['row' => 0, 'column' => 0, 'sticky' => 'w']);
$input->grid(['row' => 0, 'column' => 1]);

// Place — absolute position
$badge->place(['x' => 20, 'y' => 20]);

$button = new Button($window->getId(), [
    'text'   => 'Save',
    'bg'     => '#4CAF50',
    'fg'     => 'white',
    'font'   => 'Helvetica 14 bold',
    'relief' => 'raised',
    'padx'   => 12,
    'pady'   => 6,
]);

// Update at runtime
$button->setBackground('#2196F3');
$label->setText('Saved!');

// File picker
$file = TopLevel::getOpenFile();

// Directory picker
$dir = TopLevel::chooseDirectory();

// Color picker
$color = TopLevel::chooseColor();

// Message box — returns 'ok', 'cancel', 'yes', 'no'
$result = TopLevel::messageBox('Are you sure?', 'yesno');

$menu     = new Menu($window->getId(), ['type' => 'main']);
$fileMenu = $menu->addSubmenu('File');

$fileMenu->addCommand('New',  fn() => newFile());
$fileMenu->addCommand('Open', fn() => openFile());
$fileMenu->addSeparator();
$fileMenu->addCommand('Exit', fn() => exit(), ['foreground' => 'red']);

$editMenu = $menu->addSubmenu('Edit');
$editMenu->addCommand('Copy',  fn() => copy());
$editMenu->addCommand('Paste', fn() => paste());


PhpGui\Application;
use PhpGui\Widget\{Window, Label, Button, Input, Menu, TopLevel};

$app    = new Application();
$window = new Window(['title' => 'Demo', 'width' => 500, 'height' => 400]);

// Menu bar
$menu     = new Menu($window->getId(), ['type' => 'main']);
$fileMenu = $menu->addSubmenu('File');
$fileMenu->addCommand('Open', function () use (&$status) {
    $file = TopLevel::getOpenFile();
    if ($file) $status->setText("Opened: " . basename($file));
});
$fileMenu->addSeparator();
$fileMenu->addCommand('Exit', fn() => exit());

// Input + button
$input = new Input($window->getId(), ['text' => 'Type something...']);
$input->pack(['pady' => 10, 'padx' => 20, 'fill' => 'x']);

$status = new Label($window->getId(), ['text' => 'Ready', 'fg' => '#666']);
$status->pack(['pady' => 5]);

$btn = new Button($window->getId(), [
    'text'    => 'Submit',
    'bg'      => '#2196F3',
    'fg'      => 'white',
    'command' => function () use ($input, $status) {
        $status->setText('You typed: ' . $input->getValue());
    },
]);
$btn->pack(['pady' => 10]);

$app->run();


PhpGui\Application;
use PhpGui\Widget\WebView;

$app = new Application();
$wv  = new WebView(['title' => 'My App', 'width' => 900, 'height' => 600]);

$wv->setHtml('<h1 style="font-family:sans-serif">Hello from PHP + HTML!</h1>');

$wv->onClose(fn() => $app->quit());
$app->addWebView($wv);
$app->run();

// PHP: register a handler
$wv->bind('getUser', function (string $reqId, string $args) use ($wv): void {
    $id   = json_decode($args, true)[0];
    $user = getUserFromDatabase($id);
    $wv->returnValue($reqId, 0, json_encode($user));
});

// PHP: emit an event
$wv->emit('orderUpdated', ['id' => 99, 'status' => 'shipped']);

$wv->serveFromDisk(__DIR__ . '/frontend/dist');

// In dev: hot-reloads via the Vite dev server (HMR works)
// In prod: loads dist/ from disk — no server needed
$wv->serveVite(__DIR__ . '/frontend/dist');

$wv->enableFetchProxy();  // add this before serveFromDisk() / serveVite()


PhpGui\Application;
use PhpGui\Widget\WebView;

$app = new Application();
$wv  = new WebView(['title' => 'My Vite App', 'width' => 1024, 'height' => 768]);

$wv->enableFetchProxy();
$wv->serveVite(__DIR__ . '/frontend/dist');

// Expose a PHP function to JavaScript
$wv->bind('readFile', function (string $reqId, string $args) use ($wv): void {
    $path    = json_decode($args, true)[0];
    $content = is_file($path) ? file_get_contents($path) : null;
    $wv->returnValue($reqId, 0, json_encode($content));
});

$wv->onClose(fn() => $app->quit());
$app->addWebView($wv);
$app->run();
ini
> ; php.ini
> extension=ffi
> ffi.enable=true
> 
bash
composer 
bash
php app.php