PHP-GTK
Monday, November 24, 2014 
download | documentation | applications | faq | changelog | resources 


search for in the  


previousObject Oriented Programming
A little more complexitynext

Last updated: Sun, 12 May 2013
view this page in English

I'm going to assume that you are already familiar with object oriented programming, if you're not, I suggest you get familiar with it before proceeding with this tutorial. You may also want to look in the PHP5 manual's section on object orientation so that all this makes more sense to you.

We'll begin with "porting" the simple Hello World! program to an object oriented nature, so that you can grasp the concept quickly. The first thing you have to do is to create a class. The most easy way to make your class is to extend one of the PHP-GTK 2 classes. Now, most applications have a GtkWindow as their top level widget. So does our Hello World program. So what we're going to do is to create a class that extends GtkWindow, so that it makes our job easier:

<?php
 
class Hello extends GtkWindow
{
    // code goes here.
}
 
?>
Now we need to design the constructor of our class (a function that is called when an object of our class is created). Let's think about what we need to put in the constructor. Since the constructor is the first thing to be called when an object of our class is created, we must put all the design aspects of our program in the constructor. That means that the constructor is the place where we create all the widgets that we require, design our layout (see Packing Widgets) and finalize how our application should look.

Now, our class extends GtkWindow, but that doesn't mean that GtkWindow's constructor is called automatically. This is the default behavior of PHP5 and hence we must call GtkWindow's constructor explicitly, using the parent keyword:

class Hello extends GtkWindow
{
    function __construct()
    {
        parent::__construct();
    }
}
This means that a GtkWindow will be created (with no parameters passed to its constructor) whenever an object of our class "Hello" is created. Now the question of how to access the newly created window arises. The this keyword comes to our help! Using that keyword we can modify all aspects of our newly created window:
function __construct()
{
    parent::__construct();
    $this->set_title('Hello World!');
    $this->connect_simple('destroy', array('gtk', 'main_quit'));
}

Since this is a very simple program, we can finish all our tasks in the constructor itself. We simply put the code that we put in our procedural program into the constructor here. So we have the final program:

Example 8.1. Hello World - The Object Oriented Style

<?php
 
class Hello extends GtkWindow
{
    function __construct()
    {
        parent::__construct();
        $this->set_title('Hello World');
        $this->connect_simple('destroy', array('gtk', 'main_quit'));
 
        $label = new GtkLabel("Just wanted to say\r\n'Hello World!'");
 
        $this->add($label);
        $this->show_all();
    }
}
 
new Hello();
Gtk::main();
 
?>
Although this is hardly a great example to show the way to program in an object-oriented manner, it should help most of you get started. We'll look into some of the more advanced concepts now.


User Contributed Notes
tutorials.objectorientation.basics.php
add a note about notes
There are no user contributed notes for this page.


previousObject Oriented Programming
A little more complexitynext

Last updated: Sun, 12 May 2013
view this page in English


credits 

PHP  Copyright © 2001-2014 The PHP Group
 All rights reserved.
Last updated: Sun May 12 20:51:01 2013 CEST