Oihana PHP

ensureObjectPath.php

Table of Contents

Functions

ensureObjectPath()  : object
Ensures that a given property of an object is initialized as an object.

Functions

ensureObjectPath()

Ensures that a given property of an object is initialized as an object.

& ensureObjectPath(object &$current, string $segment) : object

If the property does not exist or is not an object, it will be replaced with a new stdClass instance. The function returns a reference to the nested object, allowing direct modification.

This is useful when building or navigating nested object structures dynamically.

Parameters
$current : object

The current object in which the property is ensured.

$segment : string

The property name to ensure as an object.

Tags
example
$data = new stdClass();
$ref =& ensureObjectPath($data, 'config');
$ref->enabled = true;
// $data now contains: (object)['config' => (object)['enabled' => true]]
author

Marc Alcaraz (ekameleon)

since
1.0.0
Return values
object

A reference to the ensured nested object (stdClass).


        
On this page

Search results