Variables
Variable declaration
Variables in Zymba are identified by the $ prefix and declared using simple assignment:
$name = "John";
$age = 30;
$active = true;
Key rules:
- Variable names must start with
$ - Names can contain letters, numbers, and underscores
- Names are case-sensitive (
$Nameand$nameare different variables) - Variables are dynamically typed
$validName = "test"; // Valid
$_private = 42; // Valid
$$invalid = true; // Invalid — only one $ prefix
$123abc = "test"; // Invalid — cannot start with a digit after $
Variable scope
Functions in Zymba have isolated scope — they cannot access variables from the surrounding context. Variables declared inside a function are local to that function:
$outer = "I am outer";
function $testScope() {
$local = "I am local";
return $outer; // $outer is NOT accessible — returns null
}
// $local is not accessible here either
To use outer variables inside a function, pass them as parameters or capture them with the use clause:
$rate = 0.08;
// Pass as parameter (preferred)
function $addTax($price, $rate) {
return $price * (1 + $rate);
}
// Capture with use (closures only)
$addTax = function($price) use ($rate) {
return $price * (1 + $rate);
};
Service parameters
In ZeyOS services, parameters passed to the service are available with the @ prefix (without a dollar sign):
// In a remotecall service that receives "token" and "secret" parameters:
$jwt = @Crypt.validateJWT(@token, @secret);
This is distinct from static class access (@Crypt, @String, etc.) — when a name after @ matches a defined service parameter, it resolves to that parameter value.
Application settings
Access per-instance application configuration through @APPSETTINGS:
$config = @APPSETTINGS.myapp;
$apiUrl = $config.apiUrl ?? "https://api.default.com";
$timeout = $config.timeout ?? 30;
The include statement
Import standard library modules and shared resources at the top of each file:
// Import built-in modules (comma-separated)
include 'zymba:sql,var,http,string,date,crypt';
// Import application resources
include 'resource:lib'; // Loads resources/lib.zy
include 'resource:helpers'; // Loads resources/helpers.zy
The zymba: prefix loads built-in modules. The resource: prefix loads shared code from the application's resources/ directory.
Always place include statements at the top of the file, before any other code.
The with statement
The with statement provides automatic resource management. The context is entered at the start of the block and automatically exited when the block completes — even on errors:
// Temporarily switch timezone
with (new @Date.TimezoneContext("UTC")) {
echo @Date.format("Y-m-d H:i:s"); // Formatted in UTC
}
// Timezone automatically restored to default
// Database transaction — auto-rolls back on error
with (new @ZeyOS.TransactionContext) {
$order = new @ZeyOS.ObjectTransaction();
$order.setFields([name: "New Order"]);
$order.save();
// If save() throws, transaction is rolled back
}
Available context classes:
@Date.TimezoneContext— temporarily change the active timezone@ZeyOS.TransactionContext— wrap database operations in a transaction@SQL.TransactionContext— SQL-level transaction context@SQL.SavepointContext— nested savepoint within a transaction@IO.LockContext— file locking