Skip to content
Jefferson González edited this page Jul 7, 2015 · 8 revisions

To use wxPHP you need to download a build for your operating system or compile the source as the building instructions describe.

Once you have the wxPHP extension file place it on the ext directory of your PHP installation. To automatically load the extension each time php is run you can add the following lines into the php.ini file:

; load php wxwidgets module
extension=wxwidgets.so

On windows this would look like this:

; load php wxwidgets module
extension=php_wxwidgets.dll

Note: It isn't required to enable the wxwidgets extension on the php.ini if you downloaded one of the official packages, which include a wxphp launcher with the wxwidgets extension enabled by default.

Structure of a wxPHP application

Create a file named myapp.php and add the code below.

<?php

class MainFrame extends wxFrame
{
  function onQuit()
  {
    $this->Destroy();
  }

  function onAbout()
  {
    $dlg = new wxMessageDialog(
        $this,
        "Welcome to wxPHP!!\nBased on wxWidgets 3.0.0\n\nThis is a minimal wxPHP sample!",
        "About box...",
        wxICON_INFORMATION
    );

    $dlg->ShowModal();
  }

  function __construct()
  {
     parent::__construct(null, null, "Minimal wxPHP App", wxDefaultPosition, new wxSize(350, 260));

     $mb = new wxMenuBar();

     $mn = new wxMenu();
     $mn->Append(2, "E&xit", "Quit this program");
     $mb->Append($mn, "&File");

     $mn = new wxMenu();
     $mn->AppendCheckItem(4, "&About...", "Show about dialog");
     $mb->Append($mn, "&Help");

     $this->SetMenuBar($mb);

     $scite = new wxStyledTextCtrl($this);

     $sbar = $this->CreateStatusBar(2);
     $sbar->SetStatusText("Welcome to wxPHP...");

     $this->Connect(2, wxEVT_COMMAND_MENU_SELECTED, array($this,"onQuit"));
     $this->Connect(4, wxEVT_COMMAND_MENU_SELECTED, array($this,"onAbout"));
  }
}

class MyApp extends wxApp 
{
  function OnInit()
  {
    $this->mf = new mainFrame();
    $this->mf->Show();

    return 0;
  }

  function OnExit()
  {
    return 0;
  }
}

$app = new MyApp();
wxApp::SetInstance($app);
wxEntry();	

?>

As of version 3.0.0.1 of wxPHP the use of wxApp to initialize an application is optional (at least under linux), so previous code would look as follows:

<?php

class MainFrame extends wxFrame
{
  function onQuit()
  {
    $this->Destroy();
  }

  function onAbout()
  {
    $dlg = new wxMessageDialog(
        $this,
        "Welcome to wxPHP!!\nBased on wxWidgets 3.0.0\n\nThis is a minimal wxPHP sample!",
        "About box...",
        wxICON_INFORMATION
    );

    $dlg->ShowModal();
  }

  function __construct()
  {
     parent::__construct(null, null, "Minimal wxPHP App", wxDefaultPosition, new wxSize(350, 260));

     $mb = new wxMenuBar();

     $mn = new wxMenu();
     $mn->Append(2, "E&xit", "Quit this program");
     $mb->Append($mn, "&File");

     $mn = new wxMenu();
     $mn->AppendCheckItem(4, "&About...", "Show about dialog");
     $mb->Append($mn, "&Help");

     $this->SetMenuBar($mb);

     $scite = new wxStyledTextCtrl($this);

     $sbar = $this->CreateStatusBar(2);
     $sbar->SetStatusText("Welcome to wxPHP...");

     $this->Connect(2, wxEVT_COMMAND_MENU_SELECTED, array($this,"onQuit"));
     $this->Connect(4, wxEVT_COMMAND_MENU_SELECTED, array($this,"onAbout"));
  }
}

$mf = new mainFrame();
$mf->Show();

wxEntry();	

?>

Running the application

After adding the above code to the file, in your command prompt cd in to the directory where the myapp.php file resides and execute:

php myapp.php

Or, if you installed from one of the official packages, a wxphp script should be available which executes wxphp scripts correctly without the need to enable the wxwidgets library on the php.ini file.

wxphp myapp.php

Windows

if you use the wxphp windows installer go to the start menu, open the wxphp prompt, and execute:

wxphp myapp.php

General Note: If your command prompt tells you that php was not found then you need to add the path to php binaries into the PATH environment variable.