Function: instance()

function instance(string|object $interface_or_class, string $context = "default"): object;

The method instance creates an object that support the specified interface.

Details

By each request, a new object is created. If you request data objects like User, a separate instance must be created for each item.

Parameters

Name Pass type Value type Default value Description
$interface_or_class by value string|object

Name of the class/interface as string or the class/interface.

$context by value string "default"

The context of binding. You can introduce a different context, and bind another implementation. Then, you can request another instance specifying the context explicitly.

Returns

object

Returns object of the class bound to the interface.

Throws

\Exception

It might throw the following exceptions in the case of any errors:

  • if the interface or class is not specified.
  • if the interface or class does not exist.
  • if the check of the classes and interfaces fails.

Authors

Oleg Schildt

Package

Source code

See also