Oihana PHP

BindsTrait

Provides logic for managing bind parameters used in PDO statements.

Allows defining a default set of bind values and dynamically merging them with runtime-provided parameters via the prepareBindVars() method.

Usage example:

class MyModel
{
    use BindsTrait;
}

$model = new MyModel();
$model->binds = [ 'id' => 42 ];

$params = $model->prepareBindVars
([
    'binds' => [ 'status' => 'active' ]
]);

print_r($params);
// Output:
// [
//     'id'     => 42,
//     'status' => 'active'
// ]
Tags
author

Marc Alcaraz (ekameleon)

since
1.0.0

Table of Contents

Constants

BINDS  : string = 'binds'
The `binds` key used in initialization and runtime parameter arrays.

Properties

$binds  : array<string|int, mixed>|null
The default bind values of the model.

Methods

initializeBinds()  : static
Initializes the `$binds` property from an initialization array.
prepareBindVars()  : array<string|int, mixed>
Prepares the binding parameters to inject into a PDO statement.

Constants

BINDS

The `binds` key used in initialization and runtime parameter arrays.

public string BINDS = 'binds'

Properties

$binds

The default bind values of the model.

public array<string|int, mixed>|null $binds = []

These values are always merged first by prepareBindVars() and may be overridden by runtime parameters. May be null to indicate "no default binds".

Methods

initializeBinds()

Initializes the `$binds` property from an initialization array.

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

The value is read from the self::BINDS key when present; otherwise the current value of $binds is kept.

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

Initialization options, typically the model constructor payload.

Return values
static

The current instance, for fluent chaining.

prepareBindVars()

Prepares the binding parameters to inject into a PDO statement.

public prepareBindVars([array<string|int, mixed> $init = [] ]) : array<string|int, mixed>

The default $binds of the model are merged first, then overridden by the runtime values found under the self::BINDS key of $init. Keys present in both win on the runtime side.

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

Runtime parameters; the values under the binds key are merged on top of the defaults.

Tags
example
class MyModel
{
    use BindsTrait;
}

$model = new MyModel();
$model->binds = [ 'id' => 42 ];

$params = $model->prepareBindVars([ 'binds' => [ 'status' => 'active' ] ]);
// $params === [ 'id' => 42 , 'status' => 'active' ]
Return values
array<string|int, mixed>

The merged associative array of bind variables ready to bind on a prepared statement.

On this page

Search results