Skip to main content

Variables

This document serves as an example to explain the usage of the given PHP script. The script defines a class, Variables, within the namespace PINGOJS, designed for managing and replacing variables and global data. Below, we detail the key functionalities and their usage.


Namespace

The script resides in the namespace:

use PINGOJS\Variables;

This allows the Variables class to be integrated seamlessly within the PINGOJS ecosystem.

Description

The Variables class provides functionality for: Managing a list of key-value pairs representing variable paths. Replacing placeholders in strings or arrays with the associated paths. Storing and retrieving global variables using a structured key system.

Example Usage

$variables = new Variables();

Description: Creates an instance of the Variables class, initializing the $list property.

Replacing Placeholders in a String

$content = "The path is core:myfile.txt";
echo $variables->replace($content);

Output: Replaces core: with the corresponding path from $list.

Processing an Array

$contentArray = ["core:config.json", "workdir:logs/error.log"];
$result = $variables->replaceArray($contentArray);
print_r($result);

Output: Returns an array with placeholders replaced by their respective paths.

Setting Global Variables

$variables->globalvar("core:status", "active");
echo $GLOBALS['core']['status'];

Output: Sets a nested global variable and prints active.

Summary

This script demonstrates effective techniques for managing variable replacement and global state using structured and modular methods. Use the described methods for path substitutions and dynamic configuration in larger PHP applications.