PHP
Article

3 Ways to Develop Cross Platform Desktop Apps with PHP

By Bruno Skvorc

PHP as a cross-platform desktop app development language? Blasphemy! Nonetheless, it’s possible.

A few years ago, everything those interested in bringing PHP to the desktop had had was the now long abandoned GTK PHP. Since then, new players have appeared, though let’s first answer the “why”.

Why?

Whyyyy_461298_679209

Why would anyone develop cross platform PHP apps for the desktop? Why not opt for something that can actually tie into the low level APIs of the operating system, like Adobe AIR? Why not go with something outdated and bloated but reliable, like Java? Why not make it a Chrome app and if you need native support, use Native Client? Hell, if you want a scripting language, why not just go with Python? Everything goes, as long as we avoid having to bundle a server with the whole shebang, right?

Off the top of my head, I can think of several far fetched scenarios:

  1. You need a good middle ground between easy syntax and good structure, which is PHP, and you can’t be bothered to learn new languages like ActionScript
  2. You’re running IT in a company with highly computer illiterate people, and the only way to force them into using a good browser for your company app is to embed it into the app you deliver. It’s still a web app, but opens in a headless Chrome!
  3. You want to avoid paying hosting costs for your own personal application, and you like to carry it with you on a USB stick. You just plug it in, run it, and your app is there – using the same SQLite DB from before. If you need to sync online, you send the whole DB export to Dropbox or some such service at the click of a button, thus making sure you’re literally the only one who can access your “web app” even without your computer.
  4. You don’t need low level OS API access – you just want to make a browser based game, or a helper app, or something similarly simple. PHP is perfectly fine for that, and you already know the language.

These scenarios might look like grasping at straws, and indeed, I really can’t think of a REAL, practical reason to want to do it that doesn’t have a viable alternative. Still, it’s nice to know it’s possible. Let’s see how.

1. Nightrain

Nightrain is a pre-packaged set of PHP-hosting prerequisites powered by PHP 5.5.x at the moment. It’s a packager written in Python that uses PHP’s internal server to host your app, thus avoiding Apache and Nginx and minimizing configuration shenanigans. However, this also means some more advanced aspects are unavailable, and you can only really use it for very rudimentary apps.

Another big con is that on Windows, a command prompt is launched first, and then the “app”. The command windows must stay open if you want to use the app, and this might be more than a little confusing to the technically illiterate people of scenario 2) above.

01

What’s more, you can only run one nightrain app by default, because it actually launches a server on port 8000 and then makes the headless browser that opens “secretly” visit localhost:8000. If you want to launch several different nightrain apps, you need to change the port in settings.ini. This also means that simply visiting localhost:8000 in your host machine’s browser will show you the same app.

Nightrain is compatible with most PHP apps/frameworks out of the box, as long as you change the database to SQLite, which is what’s used, and tweak the bundled php.ini for some missing extensions, if any. MySQL is not bundled and installing it alongside the regular stack is no simple matter. It’s very simple to make the app send the SQLite data upstream to a server you use for a centralized database anyway, so using only SQLite on the system where the app is running is somewhat logical.

By far the biggest drawback of the app is that it uses WX widgets to power the headless browser, and on Windows, this seems to come down to IE7. Changing it seems possible, by means of WXPython as mentioned in the issue linked above, but hasn’t yet been attempted. One can only hope the browser object will be updated to something more usable some time soon – until then, and until all the other critical drawbacks are fixed, I can’t even begin to imagine a use for Nightrain.

2. WXPHP

wxPHP stands for “wxWidgets for PHP” and is a PHP extension that wraps the wxWidgets library, which allows to write multi-platform desktop applications that make use of the native graphical components available to the different platforms. – Wikipedia

You install wxPHP as a separate program, which then gives you support for execution of .wxphp files by simply doubleclicking on them.

01

This means your applications are mere files, and you can distribute them everywhere with ease. You can organize your code into files and classes as usual and distribute folders. The main .wxphp file can then include these other resources.

The installation comes with several examples, including one which initializes WebView and loads the wxPHP website in a wx frame. One thing to note is that with wxPHP you aren’t developing websites as you would on the web. In other words, you don’t develop offline websites, but string together various wx widgets. As such, the library has a bit of a learning curve, and you’ll be lacking the HTML5 features you might be used to, or the simplicity of web development. There is some Proof of Concept of the internal PHP server running and serving requests, but that’s experimental and complex, and once again exposes the localhost, just like Nightrain.

wxPHP also comes with an adorable form building tool which will help you automatically generate the PHP code you need for your wxPHP apps by means of a wysiwyg editor.

02

03

Before you dismiss wx as trivial, people have developed more than basic apps in it. For example, here’s a PHP Editor with remote debugging and a plugin API.

If you’re serious about PHP desktop development, wxPHP is by far the better option when compared to Nightrain, even though Nightrain lets you write good old HTML for GUI.

One of the biggest advantages of wx here is the fact that once installed, all .wxphp files can be run at the click of the mouse. No additional installs, no awkward console windows. For technically illiterate people, that’s a godsend – you can easily distribute the app inside your company via a simple email, and the update procedure is as simple as overwriting a file.

3. TideSDK

TideSDK has a somewhat different approach than the above two. You install an SDK to be able to develop applications, and each platform has certain prerequisites. TideSDK is actually the renamed Titanium Desktop project. Titanium remained focused on mobile, and abandoned the desktop version, which was taken over by some people who have open sourced it and dubbed it TideSDK.

Once installed as per the Getting Started guide, and once we have the TideSDK Developer app (a helper application which will guide us in bundling our application into a distributable package), we can get started developing. Apps you build with Tide (via the helper app, or via the command line) will be both distributable as purely executable, or can be distributed as installable packages which get the whole “app” treatment, including an installation procedure embedded, making them uninstallable via Add/Remove Programs on Windows or your package managers on other operating systems.

Applications resources are used in conjunction with a WebKit client and a familiar and extensive API. The API is privileged, providing filesystem access that allows you to read and manage files. APIs are also provided to create and interact with a local database. Network API allows to create clients and servers or to interface with HTTP at a much lower level. It is also possible to open socket connections to other services.

Generally, TideSDK uses HTML, CSS and JS to render applications, but it supports scripted languages like Python, Ruby and PHP as well. The engine behind the rendering is WebKit which means it’ll be somewhat slow to start, but it’ll support the latest web technologies.

The heart of TideSDK is an object bridge compiled into the WebKit component. The bridge allows other scripting languages – python, php or ruby – to run on the HTML page using script tags in the DOM, just like JavaScript. You can also directly call .py, .rb or .php files from within your application.

PHP is activated by adding a module statement to the manifest file, like so:

#appname:HelloWorld
#appid:com.tidesdk.helloworld
#publisher:Software in the Public Interest (SPI) Inc
#image:default_app_logo.png
#url:http//tidesdk.org
#guid:845e9c3c-c9ff-4ad4-afdf-9638092f044f
#desc:Sample Hello World application
#type:desktop
runtime:1.3.1-beta
app:1.3.1-beta
codec:1.3.1-beta
database:1.3.1-beta
filesystem:1.3.1-beta
media:1.3.1-beta
monkey:1.3.1-beta
network:1.3.1-beta
platform:1.3.1-beta
process:1.3.1-beta
ui:1.3.1-beta
worker:1.3.1-beta
php:1.3.1-beta

Note that using the script modules for scripting languages will incur significant performance penalties on the installation and runtime of your app(s).

Interestingly, TideSDK features an object bridge which lets you, when using PHP in your apps, convert data seamlessly from JS to PHP and back. You can read more here, but a detailed TideSDK tutorial is coming soon.

There are several major downsides to using TideSDK for PHP desktop app development:

  1. The PHP development workflow is severely underdocumented and highly susceptible to bugs, but almost impossible to debug.
  2. The bundled PHP version is terribly outdated – version 5.3.X at the time of this writing. While it’s relatively easy to replace it with an up-to-date one through the /modules folder in the SDK’s installation directory, it’s an additional nuisance and lacks many modern PHP features which might come in handy in desktop app development, not to mention the built-in server which also might get an esoteric use case here.
  3. There is a learning curve. The DOM API is different from what you may be used to in web development. To echo something on screen, you would need to call $document->write() rather than echo. It’s a minor difference, but it isn’t well documented and can trip you up.
  4. By far the biggest downside is the compilation. The package you get by building an app is bound to the platform you’ve built it on. To build the app for multiple environments, you need to HAVE those multiple environments. The Windows/Linux disparity is easily solved with virtual machines (though easier to solve if your host is Windows and you have Linux VMs than the other way around), but good luck compiling it for OS X unless you’ve got an OS X device, too.

TideSDK is a neat option, but it’s far from usable. It’ll do great for HTML/CSS/JS delivery, but when it comes to PHP, I believe wxPHP is still your best bet.

Other options

There are other options available too, but they’re underwhelming to say the least.

  • PHPDesktop is similar to Nightrain, but is Windows only. On the other hand, it comes bundled with the Mongoose server and can thus execute parallel requests. It also runs Chrome, which means it’s slow to render/open but supports the latest web technologies. Its Windows-exclusivity disqualifies it from this list of multi-platform solutions, though.

  • Webinder and PHP GTK are both severely outdated and probably shouldn’t be used. In addition to that, Webinder is Windows-only.

Conclusion

While the reasons for building PHP apps for the desktop vary from use case to use case and often aren’t easy to justify, I believe it’s good to know the option is there, should you ever need it.

Have you developed desktop apps in PHP? If so, please, please let me know why and which tech you’ve used – I would absolutely love to have you write about your experiences. Know of any other options? Let me know in the comments!

Free Guide:

7 Habits of Successful CTOs

"What makes a great CTO?" Engineering skills? Business savvy? An innate tendency to channel a mythical creature (ahem, unicorn)? All of the above? Discover the top traits of the most successful CTOs in this free guide.

  • SoltaniAchraf

    I think you missed Phalanger

    • http://www.bitfalls.com/ Bruno Skvorc

      Does not apply here at all, but still an interesting find, cheers

  • Eric G

    It is really too bad nobody made PHP a compilable language that could be used like Ruby or C# so we could write real desktop applications as well as web apps.

  • ElDerecho

    I like PHP, I use it every day, I defend it online from the h8ters. But I also have a couple decades of experience writing desktop apps. PHP is optimized for web use. The IDE’s designed for desktop apps, like Visual Studio and Delphi, are light years ahead of what you would experience trying to cobble together a desktop app in PHP.

  • Stéphane Mourey

    Well, PHP-QT is another *outdated* option…

  • Taylor Ren

    Once I tried to develop PHP desktop application with Titamium. The main reason is to use web services, like retrieving a timeline from a SNS site. With PHP, the features essential to a SNS app are easy to be integrated, like OAuth, JSON, etc. also it is good to have HTML layout the contens. Later, as am experience, I used pure C# to see how different it could be from using PHP. Both were successful.

    This has been a long forgotten area in PHP development.

  • hot_rush

    never tried it myself, but https://github.com/jphp-compiler/jphp

  • Keith Penderis

    in my case i want to do it so a client can easily update their stock from the desktop with woocommerce , since in the beginning I wrote the converter that takes their mess of a sheet and then makes it pretty so it can be uploaded using csv suite now with wordpress json api I can post it directly to the website via desktop api and have a local db in sync with the wordpress products ect , boom stock updated and no c++ in sight.

  • Ben

    How about desktop php? https://code.google.com/p/phpdesktop

  • JeffM

    You should probably replace TideSDK with NodeWebkit.

    I had tried TideSDK first, and it certainly seemed promising, but it turned out to have a memory leak, eventually sucking up GBs of memory, which made it unusable for a real project. And what’s worse, the last project news update and the last release were two years ago. If anyone’s still working on it, they’re taking their sweet time, and in the meantime, it still has showstopper bugs.

    NodeWebkit has the same sort of premise as TideSDK. You build desktop apps with HTML5, CSS3, and JavaScript. And it’s better in several ways. 1) Active and regular releases. 2) It doesn’t try to invent a new JavaScript API for desktop. Instead, it reuses NodeJS. This is phenomenally better. You get a mature, widely used, widely tested JS API, and one that already has a large ecosystem and lots of libraries.

    • http://www.bitfalls.com/ Bruno Skvorc

      NodeWebkit doesn’t support PHP in any degree and hence falls short of the basic prerequisite set forth by this article. TideSDK is turning into TideKit and will be re-released soon, with higher stability and compatibility, hence its relative outdatedness.

  • http://www.bitfalls.com/ Bruno Skvorc

    Mentioned in “Other”, did you not read the article?

  • Matt Lantz

    I’m primarily a PHP developer but I sunk my teeth into Python and use PySide to get the UI strength of QT and python is super easy to understand with a foundation in PHP. NodeWebkit is the next one I use even though it packs an app as a 177mb+ size app. I used TideSDK but when they stopped supporting it for their paid stack I walked away, and looked for something that was getting more attention. PHP as desktop I suppose could be useful and I agree that its good to know the options but overall, it benefits you far more to try out something different, like the power of Node in NodeWebkit or Python, and in the end even as a sole IT guy for your company your company gets the benefit that another tool is in their arsenal of sales.

  • http://www.bitfalls.com/ Bruno Skvorc

    Excellent find, will look into that, thanks!

  • Vincent

    We are doing something similar to desktop deployment: we develop a web app which heavily relies on data from our customers’ own premises. Each customer has a local server with their CRM data (either in MSSQL or DBase). Since our website is a Yii2-based PHP app, we thought it would make sense to use a single stack and write our middleware (which runs on the customer’s server) as a PHP CLI app. I’m very satisfied with the result: PHP is excellent to deal with all kinds of databases and we use composer to deploy the middleware to our customers…

  • http://www.bitfalls.com/ Bruno Skvorc

    Anything open source I could look at? Or would you and your team like to write about it perhaps? Really interested in how and what you did.

    • alle_it

      Really interested, me too.

  • http://www.kylla.info Tom Hyde

    I developed a desktop program in PHP using only the built-in PHP web server and SQLite .
    It can be downloaded at http://www.kylla.info , if anyone’s interested.
    The download is for windows only, but I have gotten the program to work on a macintosh, using a different set-up.
    Basically, it starts the server, hides it, and starts a web browser pointed to the local url.
    If Chrome is installed, it uses that, with the ‘application shortcut’ option.

  • http://www.fuzzfree.com Antonis Adamakos

    I would use server2go… :) Well, not exactly “desktop” but since you run your own instance of apache/php you can get can have read/write permissions everywhere…. add also your own dlls for php… only for windows… We have successfully distributed a couple “desktop apps” in this way….

  • dr john

    Has anyone used this one http://www.exeoutput.com/
    It’s not cross platform but as I was considering a simple database app for windows, I thought I should ask.

  • Esra Erimez

    Although not specific to PHP, I have used ExtJS with great success to build browser based “desktop” applications.

    • leah

      Hi I’m looking for my cousin Esra who stayed with my family on long island when I was young… are you the right Esra Erimez?

  • Dr.Colossos

    Not sure why you refer to WinBinder as Webinder, but the link is correct.

    I agree that it is out-dated, but minor development continues (find it on github).

    There is also an object-oriented layer built on top of it, called Woody (also on github, see https://github.com/stefan-loewe/Woody). It al least runs on Linux through wine.

    And yes, it would never recommend to start any desktop application using PHP, but the best arguement to decide for PHP on the desktop is that you already have a code base to (re)use, e.g., when writing a client-side tool to interface with an existing web application written in PHP.
    Writing the client application in PHP can make sense then, because you can rely on exiting, hopefully well-tested components, and do not need to rewrite the same code/domain-logic in another language, with the same, then somewhat redundant refactor-test-deploy cycle.

    But if you want to write the next-gen audio suite, don’t pick WinBinder nor wxphp.

  • ydk2

    On wxphp forum you can find bundled stubs for Windows, OSX and Linux (currently only Ubuntu based). Bundled executable need only app.php in assets folder (on Mac in program.app/contents/Resources) in application directory and run or deploy to others.

  • http://www.malibro.com Hamidouh Semix

    Have you checked this?

    This is a comment on the gtk.php.net website

    [25-may-2015]
    Just had a short break to return stronger.

    We are gradually correcting the gtk.php.net site links.

    We have beta builds with PHP 5.4 and PHP 5.5.

    The developers that can help in php-gtk source code have the best support structure in gtihub:

    https://github.com/php/php-gtk-src

    Join us.

  • http://www.malibro.com Hamidouh Semix

    That’s a comment on the gtk.php.net website

  • zooboole

    @Bruno Skvorc thanks for this find. I really appreciate you mentioned PHP Desktop. I am part of those people who tried for long how to create desktop apps with php. Even though it looks hard and inadequate I still believe we can create wonders with these technologies combined with PHP.

    I recently created some desktop apps with PHP desktop(See example here). It was very cool and my clientele is satisfied with it. What I think we can do is to start improving them and they will give us the best. Thanks for this post.

  • Taheri

    i did it before using a portable wamp version (wampee) with custom settings and edited text and icons, also embedded a portable version of google chrome with custom settings and set program as homepage, made an shortcut to chrome on desktop by installer and that worked just fine on both windows xp and windows vista.

  • http://namalyaya.blogspot.com/ Namal Jayathunga

    What happened to the php-nightrain? Can you upload whole repository of php-nightrain with issues and wiki? Thanks.

  • http://krzysiu.net/ krzysiunet

    WTF… “One of the biggest advantages of wx (…)”. The installer just adds file association. And how Python’s better? Because it’s trendy and PHP is infamous because kid-coders?

  • Debroah Gabaldon

    Good discussion . For what it’s worth , others need to fill out a IRS 1099-MISC , my family discovered a fillable form here http://goo.gl/Cm8bpG

  • Lirezh

    I guess you didn’t understand the “wx” part of “wxPHP”.
    No, you do not require a webserver it features native windows forms and elements.
    In terms of speed, PHP7 received an extreme performance boost and is far ahead of any other scripting language.

    While you are right that people forgot what programming means and wouldn’t even know how to look at an ASM dump of an executable; that does not mean you can’t develop a decent desktop app with a scripting language

Recommended
Sponsors
Because We Like You
Free Ebooks!

Grab SitePoint's top 10 web dev and design ebooks, completely free!

Get the latest in PHP, once a week, for free.