Desktop Applications in PHP
Shared by: hedongchenchen
-
Stats
- views:
- 16
- posted:
- 11/24/2011
- language:
- English
- pages:
- 35
Document Sample


Desktop Applications in PHP
by Nirav Mehta
CEO, MagNet WebPublishing Pvt.
Ltd.
Goals for this session
• Understand what is PHP-GTK
• Basics of PHP-GTK
• Applications available
• Future
• Keep you awake!
What is PHP
• PHP Hypertext Preprocessor
• Embedded in HTML
• Available on 10 million+ domains
• Competes with Perl, ASP, JSP, CFM
• Open Source
• About 100 Extensions
• Good base for all kinds of functionality
More about PHP
• Structural Programming roots
• Increasing OO support
• Strong database support
• C + HTML
• Syntax similar to Perl
• Very easy to learn
• More of a general purpose language now
A simple PHP script
<?php
$str[] = “Hello!”;
$str[] = “How are you?”;
$str[] = “Good bye!”;
for($i=0; $i<count($str); $i++)
echo “$str[$i]<br>”;
?>
Hello
How are you?
Good bye!
What is GTK+
• GTK stands for GIMP Tool Kit
• GIMP (GNU Image Manipulation
Program) - popular image manipulation
software on Linux
• GTK: core of Gnome desktop
environment
• Ported to Windows and BeOS
• Collection of libraries that provide object
oriented framework to develop graphical
user interfaces – GTK, GDK, GLib
What is PHP-GTK
• An object-oriented PHP extension
providing bindings to GTK+
• Probably the largest PHP extension:
– 95 classes, 970 methods, 600
constants
• Developed by Andrei Zmievski
• First stable release: January 2002
• Inspired from PyGTK
What can it do?
• Developed as proof-of-concept, getting
better now
• Example Applications
– PHPMole
– Teak
– Agata Report
– NewzRider
– PhpGFtp
– and a lot more…
PHPMole – Modular IDE
Teak – IMAP Mail Client
Agata Report – GUI DB Client
NewzRider – NNTP Client
PhpGFtp – FTP Client
Other applications
• Development Related
• Games
• Internet Related
• Site Management
• Utilities
http://gtk.php.net/apps/
Web vs. Desktop
• Different ballgame
• Web
– Request / Response
– Top-down execution
– HTML / Interface limitations
• Desktop
– Event driven
– Anything possible with GUI
Hello World!
• Let’s see what it takes to do a “Hello
World” in PHP-GTK
– hello.php
Loading PHP-GTK Extension
• Typically dynamically loaded
• Determine the OS, load extension
• GTK Class
if (!class_exists('gtk')) {
if (strtoupper(substr(PHP_OS, 0, 3)) == 'WIN')
dl('php_gtk.dll');
else
dl('php_gtk.so');
}
Widgets
• Any GUI component: text box, label,
window, button
• Similar to Controls
• Derived from GtkWidget class
• Lifecycle consists of 5 parts:
– Creation
– Placement
– Signal connection
– Display
– Destruction
Lifecycle of a Widget
• Creation
<? $button = &new GtkButton('Ok');
?>
• Placement
<? $container->add($button); ?>
• Signal connection
<? $button->connect('clicked',
'on_clicked'); ?>
• Display
<? $button->show(); ?>
• Destruction
Containers
• Widget that can contain another widget
• Examples: window, table, box
• Derived from GtkContainer
• A container can contain another container,
which in turn can contain containers or
widgets!
Signals
• Signals emitted on events – click, double
click, mouse over, destroy etc.
• Both user actions and system events
• Inherited from parents, can define own
• Signal-like events generated by the
window system - expose_event,
button_press_event etc.
Callbacks
• Functions invoked on signals
• Normal PHP functions
• Every signal can be connected to one or
more callback functions
• The 1st parameter is always the widget
that emitted the signal
• Varying number of parameters depending
on the signal
• Callback can be a method of an object
Connecting signals to callbacks
• connect() or connect_object() methods
• First parameter is the signal name, second -
callback name
• Simple connection:
<? $button->connect('clicked',
'on_button_clicked'); ?>
• Multiple connections:
<?
$text->connect('changed',
'update_status');
$text->connect('changed',
'save_text');
?>
Connecting signals…
• Passing your own parameters
<?
$button->connect('clicked',
'on_button_clicked', $variable);
function
on_button_clicked($button,
$variable);
?>
• When object reference not required –
connect_object
– Associating one function with many signals or
many widgets
Sample PHP-GTK Apps
• Calendar
• File Selection
• Fonts
• Calculator
• Menu Bar
• Tree
• Notebook
• Scribble
Glade / libglade
• Building GUI by hand is complex
• Glade is GTK+ User Interface Builder
• libglade generates GTK+ widgets on
runtime using Glade’s XML files
• gladeXT – tool to convert the Glade XML
file into PHP code
• Can also use GladeXML class directly
Glade screenshot
Example Application
• So what shall we build?
PHP-GTK Extensions
• Extension support makes it easy to add
newer widgets and functionalities
• Currently available extensions:
– libglade
– scintilla - source code editing widget
– GtkHtml - HTML rendering and editing
widget
– GdkPixbuf - loading and rendering of images
– ComboButton, PieMenu, SPaned, SQPane,
ScrollPane
Limitations of PHP-GTK
• Main limitation due to current Zend Engine
– reliance on &new
– cannot invoke methods on object properties
– class properties cannot store references
– objects are not destroyed until the app is shut down
– lack of threading
• Cannot write new widgets from scratch
• IDEs not available
• Installation problems
• Everybody can see the code!
Our Experience with PHP-GTK
• PHPGale
• PhpGFtp
• Incomplete manual
• Learning curve
• Interface building
• Overall, good start
What's new on PHP-GTK
• Threads
• bcompiler
• GTK 2
• Zend Engine 2 integration
Endless Possibilities
• PHP on both web and desktop
• Backend management tools
• Reporting tools
• Palm applications
• Pitching against Java?
Resources
• http://www.php.net/
• http://gtk.php.net/
• http://www.gtk.org/
• http://gtk.miester.org/
Thank you!
Nirav Mehta
nirav@magnet-i.com
http://www.magnet-i.com
• Credits
– Andrei Zmievski
– MagNet, PhpGFTP team
Get documents about "