Skip to content

Object Functions

Note

For functions related to creating and updating records in the database, please see the record functions documentation.


object\clear()

The object\clear() function removes the value associated with a KEY from an OBJECT. This function returns a pointer to the OBJECT, although retaining the reference to the object is optional.

object\clear()
object\clear(OBJECT, KEY): OBJECT

Example:

object\clear() Example
$object = object\create();
object\set($object, 'firstName', 'Albert');
object\set($object, 'lastName', 'Einstein');
object\clear($object, 'lastName');
output\printLine($object); // Outputs: {"firstName":"Albert"}


object\cloneDeep()

The object\cloneDeep() function generates a deep copy of an OBJECT. This function returns a pointer to the new object, but maintaining the reference to the original object is not necessary.

object\cloneDeep()
object\cloneDeep(OBJECT): OBJECT

Example:

object\cloneDeep() Example
$object = object\create();
object\set($object, 'firstName', 'Albert');
object\set($object, 'lastName', 'Einstein');

$results = object\create();
object\set($results, 'Science', 'A');
object\set($results, 'Maths', 'A+');
object\set($results, 'English', 'B');

object\set($object, 'results', $results);

$object2 = object\cloneDeep($object);
$object3 = $object;

object\clear($object, 'lastName');

output\printLine($object); 
// Outputs: {"firstName":"Albert","results":{"Science":"A","Maths":"A+","English":"B"}}

output\printLine($object2); 
// Outputs: {"firstName":"Albert","lastName":"Einstein","results":{"Science":"A","Maths":"A+","English":"B"}}

output\printLine($object3); 
// Outputs: {"firstName":"Albert","results":{"Science":"A","Maths":"A+","English":"B"}}


object\create()

The object\create() function creates a new, empty object (a key-value map) and returns it. Objects can be made hierarchical by creating additional objects and assigning them to keys.

object\create()
object\create(): OBJECT

Examples:

object\create() Example 1
$object = object\create();
object\create() Example 2
$object = object\create();
object\set($object, 'firstName', 'Albert');
object\set($object, 'lastName', 'Einstein');
output\printLine($object); // Outputs: {"firstName":"Albert","lastName":"Einstein"}
object\create() Example 3
$object = object\create();
object\set($object, 'firstName', 'Albert');
object\set($object, 'lastName', 'Einstein');

$results = object\create();
object\set($results, 'Science', 'A');
object\set($results, 'Maths', 'A+');
object\set($results, 'English', 'B');

object\set($object, 'results', $results);

output\printLine($object); 
// Outputs: {"firstName":"Albert","lastName":"Einstein","results":{"Science":"A","Maths":"A+","English":"B"}}

Example 4 demonstrates the creation of a more complex object structure, including its corresponding JSON representation.

object\create() Example 4
// Define student 1 - Albert Einstein
$student1 = object\create();
object\set($student1, 'id', '0001');
object\set($student1, 'firstName', 'Albert');
object\set($student1, 'lastName', 'Einstein');

// Define student 1 results
$student1Results = object\create();
object\set($student1Results, 'Science', 'A');
object\set($student1Results, 'Maths', 'A+');
object\set($student1Results, 'English', 'B');
object\set($student1Results, 'Physics', 'A+');

// Add results to student 1 object
object\set($student1, 'results', $student1Results);


// Define student 2 - Issac Newton
$student2 = object\create();
object\set($student2, 'id', '0002');
object\set($student2, 'firstName', 'Issac');
object\set($student2, 'lastName', 'Newton');

// Define student 2 results
$student2Results = object\create();
object\set($student2Results, 'Science', 'A+');
object\set($student2Results, 'Maths', 'A+');
object\set($student2Results, 'English', 'C');
object\set($student2Results, 'Chemistry', 'C');

// Add results to student 2 object
object\set($student2, 'results', $student2Results);


// Define student 3 - Max Plank
$student3 = object\create();
object\set($student3, 'id', '0003');
object\set($student3, 'firstName', 'Max');
object\set($student3, 'lastName', 'Plank');

// Define student 3 results
$student3Results = object\create();
object\set($student3Results, 'Science', 'A');
object\set($student3Results, 'Maths', 'B');
object\set($student3Results, 'English', 'C');

// Add results to student 3 object
object\set($student3, 'results', $student3Results);


// Define student 4 - Charles Darwin
$student4 = object\create();
object\set($student4, 'id', '0004');
object\set($student4, 'firstName', 'Charles');
object\set($student4, 'lastName', 'Darwin');

// Define student 4 results
$student4Results = object\create();
object\set($student4Results, 'Science', 'B');
object\set($student4Results, 'Maths', 'C');
object\set($student4Results, 'English', 'B');

// Add results to student 4 object
object\set($student4, 'results', $student4Results);


// Define student 5 - Marie Curie
$student5 = object\create();
object\set($student5, 'id', '0005');
object\set($student5, 'firstName', 'Marie');
object\set($student5, 'lastName', 'Curie');

// Define student 5 results
$student5Results = object\create();
object\set($student5Results, 'Science', 'B');
object\set($student5Results, 'Maths', 'C');
object\set($student5Results, 'English', 'B');

// Add results to student 5 object
object\set($student5, 'results', $student5Results);


// Define student 6 - Nikola Tesla
$student6 = object\create();
object\set($student6, 'id', '0006');
object\set($student6, 'firstName', 'Nikola');
object\set($student6, 'lastName', 'Tesla');

// Define student 6 results
$student6Results = object\create();
object\set($student6Results, 'Science', 'B');
object\set($student6Results, 'Maths', 'B+');
object\set($student6Results, 'English', 'C');
object\set($student6Results, 'Physics', 'A+');

// Add results to student 6 object
object\set($student6, 'results', $student6Results);


// Create an array of student objects 
$listOfStudents = array\push($listOfStudents, $student1, $student2, $student3, $student4, $student5, $student6);

// Create a students object and add the array of students to the object
$students = object\create();
object\set($students, 'students', $listOfStudents);


output\printLine($students);
Example 4 JSON Structure
{
    "students": [
        {
            "id": "0001",
            "firstName": "Albert",
            "lastName": "Einstein",
            "results": {
                "Science": "A",
                "Maths": "A+",
                "English": "B",
                "Physics": "A+"
            }
        },
        {
            "id": "0002",
            "firstName": "Issac",
            "lastName": "Newton",
            "results": {
                "Science": "A+",
                "Maths": "A+",
                "English": "C",
                "Chemistry": "C"
            }
        },
        {
            "id": "0003",
            "firstName": "Max",
            "lastName": "Plank",
            "results": {
                "Science": "A",
                "Maths": "B",
                "English": "C"
            }
        },
        {
            "id": "0004",
            "firstName": "Charles",
            "lastName": "Darwin",
            "results": {
                "Science": "B",
                "Maths": "C",
                "English": "B"
            }
        },
        {
            "id": "0005",
            "firstName": "Marie",
            "lastName": "Curie",
            "results": {
                "Science": "B",
                "Maths": "C",
                "English": "B"
            }
        },
        {
            "id": "0006",
            "firstName": "Nikola",
            "lastName": "Tesla",
            "results": {
                "Science": "B",
                "Maths": "B+",
                "English": "C",
                "Physics": "A+"
            }
        }
    ]
}


object\get()

The object\get() function obtains the value associated with a specified KEY from an OBJECT. If the KEY does not exist, the function returns NULL.

object\get()
object\get(OBJECT, KEY): MIXED

Examples:

object\get() Example 1
$firstName = object\get($object, 'firstName');
object\get() Example 2
$object = object\create();
object\set($object, 'firstName', 'Albert');
object\set($object, 'lastName', 'Einstein');

$firstName = object\get($object, 'firstName');
output\printLine($firstName); // Outputs: "Albert"


object\has()

The object\has() function checks whether a KEY is associated with a value in an OBJECT, returning a boolean to indicate the presence of the KEY.

object\has()
object\has(OBJECT, KEY): BOOLEAN

Example:

object\has() Example
$object = object\create();
object\set($object, 'firstName', 'Albert');
object\set($object, 'lastName', 'Einstein');

output\printLine( object\has($object, 'firstName') );  // Outputs true
output\printLine( object\has($object, 'middleName') ); // Outputs false


object\set()

The object\set() function assigns a VALUE to a KEY in an OBJECT. If the KEY does not exist, it is created and assigned the VALUE. This function returns a pointer to the OBJECT, but maintaining the reference to the object is optional.

object\set()
object\set(OBJECT, KEY, VALUE): OBJECT

Examples:

object\set() Example 1
$object = object\create();
object\set($object, 'key', 'some-value');
object\set() Example 2
$object = object\create();
object\set($object, 'firstName', 'Albert');
object\set($object, 'lastName', 'Einstein');
output\printLine($object); // Outputs: {"firstName":"Albert","lastName":"Einstein"}


See Also