P4A Introduction tutorial

P4A is not a CMS, with P4A you cannot build a website... P4A is a framework to build rich applications available using a web browser. This tutorial aims to explain the basic concepts behind P4A and guide you step by step creating applications.

P4A is completely object oriented, object oriented programming helps a lot with gui/event programming and P4A does exactly this: it gives you an easy system to generate graphic interfaces based on widgets and allows to manage events. This lets you reproduce a client application in a web environment, same programming feelings and same usage feelings.

An application created with P4A has a main object that we’re going to call instance of P4A. To create a working instance of P4A you’ve to:

  • create a class that extends the main class “p4a”
  • create an instance of p4a with the singleton method of the class p4a (p4a::singleton())
  • call the main method of the class p4a ($instance→main())

Let’s create a folder where we’re going to put our project and we make it accessible by the web server. All classis we’re going to define in our applications have to be positioned in the “objects” folder, eg:

<?php
 
class Test extends P4A
{
    function Test()
    {
        $this->P4A();
        
        // more code is needed to do something... go on reading :)
    }
}
 
?>

The class extends p4a, note that the constructor has a &: all methods that return objects (also the constructor) must have the reference notation. Remember to call the parent constructor!

Now, inside test, let’s create our main file: index.php, within this file we’ve only to define some constants, create an instance of p4a and call the main method.

<?php
 
define('P4A_LOCALE', 'en_US');
require_once('../p4a/p4a.php');
 
$p4a =& P4A::singleton('Test');
$p4a->main();
 
?>

We can define many different constants, to have a complete list take a look at p4a/p4a/config.php file. The most important constants that you’ll use sooner are P4A_LOCALE, that let you define the language/locale of the application (default is en_US) and P4A_DSN to define the database connection (we’ll see it later).

Now we’ve created our first application but if you point your browser to http://localhost/test you won’t see nothing... just because we’ve not created any mask! A mask is what you would call a window in a client environment. Note that we can open more than one mask a time, but we’ll display only one a time, P4A is exclusively SDI.

A mask allows to visualize widgets using predefined template zones. The default template has 3 zones: menu, top, main. As we’ll see we’ll use a different template in extremely rare cases, we’ll probably do all with the default template.

<?php
 
class Mask1 extends P4A_Mask
{
    function Mask1()
    {
        //Let's call the parent constructor
        $this->P4A_Mask();
 
        //Let's create a new p4a_button
        $button =& $this->build('P4a_Button', 'button');
 
        //set some properties
        $button->setValue('Click here');
        $button->setWidth(100);
        $this->button->setHeight(100);
 
        //display the button in the main zone
        $this->display('main', $button);
    }
}
 
?>

In this mask we’ve created a simple button and we’ve displayed in the central zone of the template. It’s really important to take care of creating objects, we won’t ever use the standard PHP syntax ($obj = new class()), we’ll use the build method instead. All objects extending p4a can build sub-objects using the build method. This method accepts two mandatory parameters: the object type (the class, p4a_button) and the name that you want to give to the instance (button). When an object builds a sub-object, the new object will stay under the parent object as a property with the assigned name ($this→button). The build method also returns the object so you can grab it if you want to. The built object is available everywhere, in another mask we can call it using $p4a→masks→mask1→button. After setting some properties we display the object within the template with the display method of the mask.

Now we’ve our first mask, so let’s tell the application how to visualize it. Re-open test.php and insert the openMask method.

<?php
 
class Test extends P4A
{
    function Test()
    {
        $this->P4A();
        $this->openMask('Mask1');
    }
}
 
?>

Note that we have a limited set of zones to display data in a mask. That’s why there are some widgets to group and display more objects a time. Most important are frame and fieldset. These two widgets allows you to anchor other widgets using the anchor method (anchoring on a new row), anchorLeft and anchorRight (anchoring on the last row, floating on the left or on the right). We could use a frame with this syntax in mask1.php:

TO BE CONTINUED

 
tutorials/introduction.txt · Last modified: 2007/01/03 12:56
 
Creative Commons License Powered by PHP Valid XHTML 1.0 Valid CSS Driven by DokuWiki