Oihana PHP

setObjectValue.php

Table of Contents

Functions

setObjectValue()  : object
Sets a value in a flat object using a single property name.

Functions

setObjectValue()

Sets a value in a flat object using a single property name.

setObjectValue(object $document, string $key, mixed $value) : object

This helper function assigns the given value to the specified property of the provided object. It does not support nested paths or separators.

The object is returned with the updated property.

Parameters
$document : object

The object to modify.

$key : string

The property name to set.

$value : mixed

The value to assign to the property.

Tags
example
$obj = (object)['name' => 'Alice'];
$updated = setObjectValue($obj, 'age', 30);
// $updated = (object)['name' => 'Alice', 'age' => 30];
author

Marc Alcaraz (ekameleon)

since
1.0.0
Return values
object

The modified object with the new or updated property.


        
On this page

Search results