DEV Community

Cover image for Writing Linux Desktop App in PHP part 1: Introduction
David Maye
David Maye

Posted on

Writing Linux Desktop App in PHP part 1: Introduction

Brief introduction

So... I have been programming in PHP for a long time. I started doing some WordPress websites, then some custom theme and plugin development using the Root framework (alias Mix of WordPress and Laravel), until I felt in love with Symfony...

At first my Symfony code sucked very bad... I know it did, because when I read back some old project, compared to today's code, I just ask myself: "wow, why was I so ignorant? This code is so not DRY, here there is a security issue, etc...". This is also due to the fact that having the mindset of creating a small app vs a scalable open-source project is completely different (more on that on other posts, I guess).

I'm a web dev, who loves the embedded world

But I started to program web dev due to necessity. Because web dev is the only field I could start working and having real money with small experience (using WordPress for example). But I'm actually the kind of guy that prefer to have hard pain programming embedded devices than managing HTTP requests. It is so true, that I have tried a lot of programming languages just for the sake of fun. This includes:

  • C.
  • C++.
  • Rust.
  • Go.
  • Pascal.
  • Ada (before Alire package manager became so popular that you could easily find it on Google search).
  • Assembly (for Intel x84_64 architecture).

And a ton of other languages just for fun and some elitism:

  • Lisp
  • Clojure
  • Vala
  • Julia
  • Kotlin
  • Javascript
  • Python
  • Scala
  • Nim
  • Elixir / Erlang
  • Ruby

Although, I never used them to do some serious low level programming (Except Arduino C++ for some rubber-ducky scripting). Because I work on web dev, I try their full-stack web framework. And well... I have to admit that, to me, PHP is just the best language to get web things done correctly in web dev, due to its great community of dev working hardly to make web dev technologies great.

But I decide to learn GUI programming in C

Some days ago, I got a click when I discovered that GObject, GLib and GTK are very easy to use (or I just learned to read API documentation, or maybe both). It is so true, that I created my first GUI app using C and GTK, a basic window with a sidebar.

Then, I tried out using Adwaita (A GTK based library) in a way the Adwaita documentations doesn't explain it: creating a window by passing a AdwApplicationtype instead of G_Callback type. At first, I had to use Gemini to give me short explanations on how to do so (Because there are no official tutorials for it, last time I checked). But now the concept are so clear, that I don't need AI any more.

But, why create apps in C? Why not Python? Simple... I hate python because it is popular... No more explanations needed... I'm kind of elitist for no reasons I have to admit.

But not only that. I really like learning how computer works. Although I don't have time for computer science classes (like the OSSU free academy), I really like to tweak and observe. Once I can grasp at a concept, at least by simple intuition, on how X works internally, it feels wonderful. As an example:

  • Maybe GObject is the worst way to do objects in C, but I liked the process of learning and seeing GUI app appearing on my Computer Screen.
  • Yesterday I learned about Bitwise operations importance in embedded memory management, it felt wonderful (Use uint8_t + bit masks instead of n number Booleans to adjust an App state, just brilliant).

Also, learning C makes me a step closer to embedded programming. If I have hands-on production based experience on how to create a GUI App, it's like learning how to use C + something. The more I interact with see, the less scary it gets. So passing from GUI to embedded world should be easier (I guess...).

And now I want to program GUI apps in PHP

Now, If I'm so keen on learning C, why give am I giving a detour on learning to create PHP GUI Apps?
OK first, I also hate PHP... just because it is popular... But I hate Python more than PHP... So I prefer PHP more than Python...

But also, I have seen a video explaining how some projects uses compiled vs interpreted languages, and how does an Interpreter work.

And something clicked in my brain: Interpreter languages are not bad. They just ease our work and production compared to compiled languages (due mostly to skill issues and deadlines in my case).

I also understood that PHP can be as powerful as Python (in Machine Learning for example), if their developer had enough resources to do so (time + money + motivation). Because PHP can be extended as Python using C based libraries. And to push this statement further, you can also create a dedicated and optimized PHP version for any given field by just modifying it source code (imaginePHP8.5-ML).

This shift of mind made me understand that PHP is not just Yet Another Interpreted Language... it can be everything you want and need.

In theory, I can just port many Python Libraries to PHP... and make PHP the new mainstream language for virtually anything. (Although this requires a lot of technical, maintenance and marketing work... and nor maintenance and marketing are my cup of tea).

So why focus on GUI project with PHP, and not low level embedded software? Because I don't have any Arduino or similar devices. Also because, right now, I need a tool GUI to manage my VPS instances that use my Open-source ERP (more on that in other posts).

So I decided to just fork the archived PHP GTK project on GitHub to my GitLab instance and work on it to make it support GTK4.

Here is the library I forked

GitHub logo php / php-gtk-src

The PHP GTK Bindings

PHP-GTK: PHP language bindings for GTK+ toolkit
===============================================
PHP-GTK is a PHP extension that enables you to write client-side cross-platform
GUI applications. This is the first such extension of this kind and one of the
goals behind it was to prove that PHP is a capable general-purpose scripting
language that is suited for more than just Web applications
This extension will _not_ allow you to display GTK+ programs in a Web browser
and can be used in the Web environment only if you are running the webserver
locally. It is intended for creating standalone GUI applications
Requirements
============
As a prerequisite, you should have a CLI binary (command-line) version of PHP
installed along with the header files and development scripts. They should
normally be installed when you do 'make install' in the PHP source tree.

Currently, PHP-GTK requires PHP 5.1.2 or greater to run, and to build.

PHP-GTK supports GTK+
โ€ฆ

And here is my Gitlab repo

kiirokodo-group / php-gtk-src ยท GitLab

The PHP GTK Bindings

favicon gitlab.com

I think doing so will help me boost my skills. Now how does those tools such as Make/Cmake/Meson works, and how to use C to extend a language.

In the next post, I will explain how everything went, because I have to go to work. See you ๐Ÿ‘‹.

Top comments (0)