Magic around $app, $$ and components

The $app variable in the PHP applications is the 'magic' variable that contains references to almost anything in your application. If you create your applications, all your components will be assigned unique names. In general, it will use a prefix of the component type with a sequence number. I.e., for a normal edit field you would get edit_1, edit_2, edit_…. But that name could be re-used in other PHP applications. As long as these applications will run standalone, then nothing is at hand, but if you are starting to use building blocks, it is very likely to come across duplicate id's. Since HTML does not support namespaces, PHsPeed takes care of that and will prefix all component variables with its module name. So 'edit_1' might become something like: 'mymodule_edit_1'. To access the value of this component you would get:

$myvar = $app→mymodule_edit_1→value;

To access this variable in your event code, you have to think about the given name in the IDE continuously, and it's the 'real' name in the final PHP code. To prevent that, PHsPeed uses a short notation for the $app→mymodule_ part. By typing $$ as the prefix for your application, PHsPeed will automatically prefix this variable. So you can type:

$myvar = $$edit_1->value;

Which has the same effect, but is more comfortable to use. The PHP pre-compiler also accepts this notation in the IDE. So if you precompile your event code to avoid syntax errors after generation, you will not get errors on these statements.

In a lot of situations, you will need to $this→ your variable. To speed up typing this, there is a code-completion action in the PHP editor that will replace $$ by $this. This can be forced using shift-space as soon as you have typed $$.

As an example of above, the next code will logon a user, using plain SQL. PHsPeed uses PDO and prepared the code for security reasons.

function Form_1_onSubmitForm($app)
{
 $$dbquery_1 -> SetQuery('select * from phsp_user where userid=:username and userpsw=:password');
 $$dbquery_1 -> StringFieldByName(':username',$$edit_1->value);
 $$dbquery_1 -> StringFieldByName(':password',$$edit_2->value);

 // open query and return first record
 $result=$$dbquery_1 -> SimpleOpen(true);
 if($result===FALSE) {
    $$label_1->label = 'Wrong credentials';
 } else {
    setLoggedOn();
    redirect('mainmenu.php');
 }
}

After code generation this code will be modified to:

function onSubmitForm($app){
    $app->main_dbquery_1 -> SetQuery('select * from phsp_user where userid=:username and userpsw=:password');
    $app->main_dbquery_1 -> StringFieldByName(':username',$app->main_edit_1->value);
    $app->main_dbquery_1 -> StringFieldByName(':password',$app->main_edit_2->value);
    // open query and return first record
    $result=$app->main_dbquery_1 -> SimpleOpen(true);
    if($result===FALSE) {
       $app->main_label_1->label = 'Wrong credentials';
    } else {
       setLoggedOn();
       redirect('mainmenu.php');
    }
  }