Type Declarations

chung-leong edited this page Mar 21, 2013 · 11 revisions
Clone this wiki locally

Static typing is employed in PHP+QB. The basic characteristics of a variable cannot change during program execution. Its type and dimensions must be declared at the time of compilation. This information is usually stored in a function’s Doc Comment:

 * addInegers() - Adding two integer in such a way that you get the same answer no matter what  
 * @engine qb
 * @param int32 $a The first integer
 * @param int32 $b The second integer
 * @local int32 $answer
 * @return int32
function addIntegers($a, $b) {
	$answer = 42;
	return $answer;

PHP+QB uses an extension of the PHPDoc syntax. Several tags were added to satisfy requirements specific to QB--for instance, the need to declare local variables. Each tag is prefixed by the character @. Lines that do not begin with @ are ignored.

The following lists the tags used by QB:

@engine qb | qb-native | qb-bytecode

The presence of the @engine tag designates a function for processing through the QB engine. The tag should be followed by a single parameter. This parameter determines whether QB will compile the function into native code or use bytecode interpretation. If qb-native is specified, QB will try to compile the function into native code, falling back to bytecode interpretation should it fails. If the neutral qb is given, then native compilation only occurs when the configuration option qb.compile_to_native is set to true. If qb-bytecode is specified, then compilation to native code never occurs for the function.

In a typical usage scenario, you would only use @engine qb.

@param type $var [description]

Declares the type of a function parameter.

@local type $var [description]

Declares the type of a local variable.

@global type $var [description]

Declares the type of a global variable.

@static type $var [description]

Declares the type of a static variable.

@property type $var [description]

Declares the type of an object property. This tag is used in the Doc Comment of a class to declare properties accessible only through its __get()/__set() magic methods. For properties actually represented by a class variable, declare the variable’s type in its Doc Comment >using the @var tag instead.

@return type

Declares the return type of a function. If omitted, the function is assumed to return void.

@import path

The @import tag directs QB to import external code. Currently, Flash Pixel Bender kernel (.pbj file) is the only supported file type.

@var type

Declares the type of a class variable.

Declaring Multiple Variables

To help reduce cluster in functions that use many local variables, QB permits the use of regular expressions in type declarations. If w, x, y, z are all variables of type int32, you can declare them in a single line with the following:

 * @local int32 $(w|x|y|z)