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

Registers a class.

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

$slug

(string) (Required) The human-readable and catchy name of the class.

$class

(string|object|callable) (Required) The full class name or an instance of the class or a callback that will return the instance of the class.

$after_build_methods

(array) (Optional) An array of methods that should be called on the built object after the __construct method; the methods will be called each time after the instance construction.

Default value: null


Top ↑

Source

File: src/Tribe/Container.php

	function tribe_register( $slug, $class, array $after_build_methods = null ) {
		Tribe__Container::init()->bind( $slug, $class, $after_build_methods );
	}