Summary

Hope this tutorial has been of some use in getting you started with the object oriented style of programming in PHP-GTK 2. Before we wind up, a few more pointers follow.

  • If you are building an extremely large application, you may want to split it into multiple classes: one class for each module. For most applications however, a single class should suffice.
  • If you use multiple classes in your application, the question of accessing objects from one class in another arises. In this case, try to design a hierarchy: one master class with other classes extending it. If that is not possible, use global variables instead (not recommended).
  • If more than one function in your class needs to access a widget, make it a class property. However if only one function requires it, pass it as a parameter instead.
  • Always try to extend your classes from the widget class that will form the base of your class. Also remember that you need to construct that widget explicitly using the parent::__construct(); statement. You can access the methods of that widget via the this keyword.
  • Keep your functions as short as possible. The whole point of object oriented programming is modularization. If a function is beginning to get bulky, consider splitting it. A frequent occurrence of this case is when you try to design your entire (complicated) layout in the constructor itself. Split it into functions that create parts of the layout and assemble them all in the constructor instead.

Don't forget to have a look at the Gtk2_Components section in PEAR. All the packages there are made of high quality object-oriented code. Read their sources and emulate. All the best!