Oihana PHP System

PropertyTrait

Provides a `property` reference.

This trait allows a class to define a property reference that can be initialized from an associative array (commonly used for hydration or model initialization).

Usage:

class MyModel
{
    use PropertyTrait;
}

$model = new MyModel();
$model->initializeProperty(['property' => 'name']);
echo $model->property; // outputs "name"
Tags
author

Marc Alcaraz (ekameleon)

since
1.0.0

Table of Contents

Constants

PROPERTY  = 'property'
The 'property' key parameter used in initialization arrays.

Properties

$property  : string|null
The `property` reference.

Methods

assertProperty()  : static
Asserts the existence of the 'property' key.
initializeProperty()  : static
Initialize the `property` reference from an associative array.

Constants

PROPERTY

The 'property' key parameter used in initialization arrays.

public mixed PROPERTY = 'property'

When passing an array to initializeProperty(), this key will be checked to set the $property value.

Properties

$property

The `property` reference.

public string|null $property = null

This property is typically a string identifying a key or field name within the document or model context.

Methods

assertProperty()

Asserts the existence of the 'property' key.

public assertProperty() : static
Tags
throws
UnexpectedValueException

If the "property" key is not set.

Return values
static

initializeProperty()

Initialize the `property` reference from an associative array.

public initializeProperty([array<string, mixed> $init = [] ]) : static

If the array contains the key self::PROPERTY, the value will be assigned to $property. Otherwise, the property is set to null.

Parameters
$init : array<string, mixed> = []

Optional initialization array.

Tags
example
$model = new MyModel();
$model->initializeProperty(['property' => 'foo']);
echo $model->property; // outputs "foo"

// Method chaining
$model->initializeProperty(['property' => 'bar'])->someOtherMethod();
Return values
static

Returns $this to allow method chaining.


        
On this page

Search results