RPCL Hello World

From RadPHP XE2 Documentation
Jump to: navigation, search

On this page you will learn how to develop your first RPCL application: Hello World. The application will show a button, and when we click it we will get the message “Hello, world!”.

Contents

Hello World

First, we are going to read the code necessary to generate the application, using PHP toguether with the RPCL:

<?php
 
require_once("rpcl/rpcl.inc.php");
 
class MyPage extends Page
{
    function sayHello($sender, $params)
    {
      echo "Hello, world!";
    }
}
 
global $application;
global $myPage;
 
$myPage=new MyPage($application);
 
$myPage->OnShow="sayHello";
 
$myPage->show();
 
?>

You can create a new unit (File > New > Unit - PHP) and replace its content with the code above. Then, run it from Run > Run. Your default web browser will open the page, and you will see the text “Hello, world!” at the top left corner of your webpage.

Line by Line

Now that we’ve seen the code, lets look at it closer:

require_once("rpcl/rpcl.inc.php");

The first line includes the file rpcl.inc.php, which is required to use the RPCL library. It provides basic RPCL functions, such as use_unit(), which might be used to locate other files of the RPCL just from their filename.

class MyPage extends Page
{
    function sayHello($sender, $params)
    {
      echo "Hello, world!";
    }
}

This block defines a new PHP class, MyPage, that inherits from Page, which represents an HTML document. The method we defined inside MyPage, sayHello(), is meant to be an event handler. Event handlers expect two parameters, $sender and $params. The former always contains the reference to the object that called the event, and the later can be anything, usually an array with data relative to the circumstances of the event.

global $application;
global $myPage;

The code above is meant to make those two variables globally available. $application is a common variable available from every unit in our application. $myPage is the variable where we will store an instance of the class we have just defined.

$myPage=new MyPage($application);

We assign to $myPage an instance of MyPage, and pass it the application variable.

$myPage->OnShow="sayHello";

The line of code above assigns the OnShow MyPage event (inherited from Page) to the method we defined earlier inside of MyPage. Notice the name of the method is passed, as a string, instead of a pointer.

$myPage->show();

Here, show() prints the content of the page as HTML code.

The RAD Way

Of course, with RadPHP to help us not everything has to be coding. In fact, most things can be accomplished by droping components from the Tool Palette on the Designer and setting components properties from the Object Inspector.

Equivalent

To achive the same we did before the RAD way, you only need to:

  1. Go to File > New > Form - PHP. A new unit will open on the Designer.
  2. On the Object Inspector, located on the bottom left corner of the interface by default, click on the Events tab, and double-click OnShow event value field. You will be taken to the Code Editor, where a new method definition structure will have been added.
  3. On that method, write the following code: echo "Hello, world!";.

You can now run your unit from Run > Run, at watch it in action!

Label

You can achieve similar (better) results without touching a line of code!

  1. Go to File > New > Form - PHP. A new unit will open on the Designer.
  2. On the Tool Palette, locate the Label component (on the Standard category), drag it and drop it on the unit.
  3. On the Object Inspector, with the Label component selected on the Designer, set the value of the Caption property to Hello, world!.

Not only this way is faster, also you can place the text wherever you want, and change its font, color or size or color easily, everything without ever touching a line of code. Welcome to the RAD way!

Personal tools