CodeIgniter v4.5 API

Factories
in package

FinalYes

Factories for creating instances.

Factories allow dynamic loading of components by their path and name. The "shared instance" implementation provides a large performance boost and helps keep code clean of lengthy instantiation checks.

Tags
see
FactoriesTest

Table of Contents

Properties

$aliases  : array<string, array<string, class-string>>
Mapping of class aliases to their true Fully Qualified Class Name (FQCN).
$configOptions  : array<string, bool|string|null>
Explicit options for the Config component to prevent logic loops.
$instances  : array<string, array<class-string, object>>
Store for instances of any component that has been requested as "shared".
$options  : array<string, array<string, bool|string|null>>
Store of component-specific options, usually from CodeIgniter\Config\Factory.

Methods

__callStatic()  : object|null
Loads instances based on the method component name. Either creates a new instance or returns an existing shared instance.
config()  : BaseConfig|null
define()  : void
Define the class to load. You can *override* the concrete class.
get()  : object|null
Simple method to get the shared instance fast.
models()  : Model|null
reset()  : void
Resets the static arrays, optionally just for one component
setOptions()  : array<string, bool|string|null>
Normalizes, stores, and returns the configuration for a specific component
createInstance()  : void
Creates the shared instance.
getDefinedInstance()  : object|null
Gets the defined instance. If not exists, creates new one.
isConfig()  : bool
Is the component Config?
isNamespaced()  : bool
Is the class alias namespaced or not?
locateClass()  : string|null
Finds a component class
setAlias()  : void
Sets alias
verifyInstanceOf()  : bool
Verifies that a class & config satisfy the "instanceOf" option
verifyPreferApp()  : bool
Verifies that a class & config satisfy the "preferApp" option

Properties

$aliases

Mapping of class aliases to their true Fully Qualified Class Name (FQCN).

private static array<string, array<string, class-string>> $aliases = []

Class aliases can be: - FQCN. E.g., 'App\Lib\SomeLib' - short classname. E.g., 'SomeLib' - short classname with sub-directories. E.g., 'Sub/SomeLib'

[component => [alias => FQCN]]

$configOptions

Explicit options for the Config component to prevent logic loops.

private static array<string, bool|string|null> $configOptions = ['component' => 'config', 'path' => 'Config', 'instanceOf' => null, 'getShared' => true, 'preferApp' => true]

$instances

Store for instances of any component that has been requested as "shared".

private static array<string, array<class-string, object>> $instances = []

A multi-dimensional array with components as keys to the array of name-indexed instances.

[component => [FQCN => instance]]

$options

Store of component-specific options, usually from CodeIgniter\Config\Factory.

private static array<string, array<string, bool|string|null>> $options = []

Methods

__callStatic()

Loads instances based on the method component name. Either creates a new instance or returns an existing shared instance.

public static __callStatic(string $component, array<string|int, mixed> $arguments) : object|null
Parameters
$component : string
$arguments : array<string|int, mixed>
Return values
object|null

define()

Define the class to load. You can *override* the concrete class.

public static define(string $component, string $alias, class-string $classname) : void
Parameters
$component : string

Lowercase, plural component name

$alias : string

Class alias. See the $aliases property.

$classname : class-string

FQCN to be loaded

get()

Simple method to get the shared instance fast.

public static get(string $component, string $alias) : object|null
Parameters
$component : string
$alias : string
Return values
object|null

models()

public static models([string $alias = ][, array<string|int, mixed> $options = [] ][, ConnectionInterface|null &$conn = null ]) : Model|null
Parameters
$alias : string =
$options : array<string|int, mixed> = []
$conn : ConnectionInterface|null = null
Return values
Model|null

reset()

Resets the static arrays, optionally just for one component

public static reset([string|null $component = null ]) : void
Parameters
$component : string|null = null

Lowercase, plural component name

setOptions()

Normalizes, stores, and returns the configuration for a specific component

public static setOptions(string $component, array<string|int, mixed> $values) : array<string, bool|string|null>
Parameters
$component : string

Lowercase, plural component name

$values : array<string|int, mixed>

option values

Return values
array<string, bool|string|null>

The result after applying defaults and normalization

createInstance()

Creates the shared instance.

private static createInstance(string $component, string $class, array<string|int, mixed> $arguments) : void
Parameters
$component : string
$class : string
$arguments : array<string|int, mixed>

getDefinedInstance()

Gets the defined instance. If not exists, creates new one.

private static getDefinedInstance(array<string|int, mixed> $options, string $alias, array<string|int, mixed> $arguments) : object|null
Parameters
$options : array<string|int, mixed>
$alias : string
$arguments : array<string|int, mixed>
Return values
object|null

isConfig()

Is the component Config?

private static isConfig(string $component) : bool
Parameters
$component : string

Lowercase, plural component name

Return values
bool

isNamespaced()

Is the class alias namespaced or not?

private static isNamespaced(string $alias) : bool
Parameters
$alias : string

Class alias. See the $aliases property.

Return values
bool

locateClass()

Finds a component class

private static locateClass(array<string|int, mixed> $options, string $alias) : string|null
Parameters
$options : array<string|int, mixed>

The array of component-specific directives

$alias : string

Class alias. See the $aliases property.

Return values
string|null

setAlias()

Sets alias

private static setAlias(string $component, string $alias, string $class) : void
Parameters
$component : string
$alias : string
$class : string

verifyInstanceOf()

Verifies that a class & config satisfy the "instanceOf" option

private static verifyInstanceOf(array<string|int, mixed> $options, string $alias) : bool
Parameters
$options : array<string|int, mixed>

The array of component-specific directives

$alias : string

Class alias. See the $aliases property.

Return values
bool

verifyPreferApp()

Verifies that a class & config satisfy the "preferApp" option

private static verifyPreferApp(array<string|int, mixed> $options, string $alias) : bool
Parameters
$options : array<string|int, mixed>

The array of component-specific directives

$alias : string

Class alias. See the $aliases property.

Return values
bool

        
On this page

Search results