Oihana PHP

Model uses DebugTrait, ToStringTrait

A minimal, container-aware base class for all models of the library.

Model wires three foundational concerns together and leaves the data-access logic to its subclasses (most notably PDOModel, which mixes in PDOTrait):

  • Dependency injection: the PHP-DI Container passed to the constructor is stored on $container and reused by the initializers to resolve services declared by name (logger, PDO, …).
  • Debug flag: provided by DebugTrait and toggled through the debug init key.
  • Logging: a PSR-3 LoggerInterface resolved either directly or from a container id via the logger init key.

The constructor follows the library-wide "initialize from an options array" convention: every behaviour is configured through a single associative $init array whose keys are documented per initializer. Each initialize*() helper returns $this, allowing the fluent chaining used below.

Tags
example
use DI\Container;
use oihana\models\Model;

$container = new Container();

$model = new Model( $container ,
[
    'debug'  => true ,                 // enable verbose debug output
    'logger' => LoggerInterface::class // a logger instance, or its container service id
] );
author

Marc Alcaraz (ekameleon)

since
1.0.0

Table of Contents

Properties

$container  : Container
The PHP-DI container reference, used to resolve services by name throughout the model.

Methods

__construct()  : mixed
Creates a new Model instance and initializes the debug flag, the logger and the mock flag from the given options array.

Properties

$container

The PHP-DI container reference, used to resolve services by name throughout the model.

public Container $container

Methods

__construct()

Creates a new Model instance and initializes the debug flag, the logger and the mock flag from the given options array.

public __construct(Container $container[, LoggerInterface|string|null, mock?: bool|null} $init = [] ]) : mixed
Parameters
$container : Container

The DI container used to resolve services referenced by name (e.g. the logger), and stored on $container for later use.

$init : LoggerInterface|string|null, mock?: bool|null} = []

Optional initialization array with keys:

  • debug : Whether the debug mode is active (default false).
  • logger : A PSR-3 LoggerInterface instance, or the container service id of one to resolve.
  • mock : Whether the model runs in mock mode, e.g. to bypass real I/O in tests (default false).
Tags
throws
ContainerExceptionInterface

If an error occurs while retrieving an entry from the dependency-injection container.

NotFoundExceptionInterface

If no entry is found for the requested identifier in the container.

example
$model = new Model( $container , [ 'debug' => true , 'logger' => 'app.logger' ] );
On this page

Search results