Technical Docs

Search Documentation

Function: tribe_register

Registers a class.

Located at src/Tribe/Container.php (Line: 147)

Description

Each call to obtain an instance of this class made using the

tribe( $slug )

function will return a new instance; the instances are built just in time (if not passing an object instance, in that case it will work as a singleton) and on the first request. The container will call the class

__construct

method on the class (if not passing an object or a callback function) and will try to automagically resolve dependencies.

Example use:

 tribe_register( 'tec.some', 'Tribe__Some' );  // some code later...  // class is built here  $some_one = tribe( 'tec.some' )->doSomething();  // $some_two !== $some_one  $some_two = tribe( 'tec.some' )->doSomething();

Need the class built immediately? Build it and register it:

 tribe_register( 'tec.admin.class', new Tribe__Admin__Class() );  // some code later...  // $some_two === $some_one  // acts like a singleton  $some_one = tribe( 'tec.some' )->doSomething();  $some_two = tribe( 'tec.some' )->doSomething();

Need a very custom way to build the class? Register a callback:

 tribe_register( 'tec.some', array( Tribe__Some__Factory, 'make' ) );  // some code later...  // $some_two !== $some_one  $some_one = tribe( 'tec.some' )->doSomething();  $some_two = tribe( 'tec.some' )->doSomething();

Or register the methods that should be called on the object after its construction:

 tribe_singleton( 'tec.admin.class', 'Tribe__Admin__Class', array( 'hook', 'register' ) );  // some code later...  // the `hook` and `register` methods will be called on the built instance.  tribe( 'tec.admin.class' )->doSomething();

Parameters summary

(string) $slug The human-readable and catchy name of the class.
(string|object|callable) $class The full class name or an instance of the class or a callback that will return the instance of the class.
(array) $after_build_methods An array of methods that should be called on the built object after the <code>__construct</code> method; the methods will be called each time after the instance contstruction.

Return value summary

(void)

Examples

void tribe_register ( string $slug, string|object|callable $class, array $after_build_methods = null )