Class ObjectCollection

Deals with Collections of objects. Keeping registries of those objects, loading and constructing new objects and triggering callbacks. Each subclass needs to implement its own load() functionality.

All core subclasses of ObjectCollection by convention loaded objects are stored in $this->_loaded. Enabled objects are stored in $this->_enabled. In addition, they all support an enabled option that controls the enabled/disabled state of the object when loaded.

Direct Subclasses

Abstract
Package: Cake\Utility
Since: CakePHP(tm) v 2.0
Copyright: Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
License: MIT License
Location: Cake/Utility/ObjectCollection.php

Properties summary

  • $_enabled protected
    array
    List of the currently-enabled objects
  • $_loaded protected
    array
    A hash of loaded objects, indexed by name
  • integer
    Default object priority. A non zero integer.

Method Summary

  • __get() public
    Provide public read access to the loaded objects
  • __isset() public
    Provide isset access to _loaded
  • attached() public
    Gets the list of attached objects, or, whether the given object is attached
  • disable() public

    Disables callbacks on an object or array of objects. Public object methods are still callable as normal.

  • enable() public
    Enables callbacks on an object or array of objects
  • enabled() public
    Gets the list of currently-enabled objects, or, the current status of a single objects
  • load() abstract public
    Loads a new object onto the collection. Can throw a variety of exceptions
  • loaded() public
    Gets the list of loaded objects, or, whether the given object is loaded
  • normalizeObjectArray() public static

    Normalizes an object array, creates an array that makes lazy loading easier

  • prioritize() public
    Prioritize list of enabled object
  • set() public
    Adds or overwrites an instantiated object to the collection
  • setPriority() public
    Set priority for an object or array of objects
  • trigger() public

    Trigger a callback method on every object in the collection. Used to trigger methods on objects in the collection. Will fire the methods in the order they were attached.

  • unload() public
    Name of the object to remove from the collection

Method Detail

__get()source public

__get( string $name )

Provide public read access to the loaded objects

Parameters

string $name
Name of property to read

Returns

mixed

__isset()source public

__isset( string $name )

Provide isset access to _loaded

Parameters

string $name
Name of object being checked.

Returns

boolean

attached()source public

attached( string $name null )

Gets the list of attached objects, or, whether the given object is attached

Deprecated

3.0.0 Will be removed in 3.0. Use loaded instead.

Parameters

string $name optional null

Optional. The name of the object to check the status of. If omitted, returns an array of currently-attached objects

Returns

mixed

If $name is specified, returns the boolean status of the corresponding object. Otherwise, returns an array of all attached objects.


disable()source public

disable( string|array $name )

Disables callbacks on an object or array of objects. Public object methods are still callable as normal.

Parameters

string|array $name
CamelCased name of the objects(s) to disable (string or array)

enable()source public

enable( string|array $name , boolean $prioritize true )

Enables callbacks on an object or array of objects

Parameters

string|array $name
CamelCased name of the object(s) to enable (string or array)
boolean $prioritize optional true
Prioritize enabled list after enabling object(s)

enabled()source public

enabled( string $name null )

Gets the list of currently-enabled objects, or, the current status of a single objects

Parameters

string $name optional null

Optional. The name of the object to check the status of. If omitted, returns an array of currently-enabled object

Returns

mixed

If $name is specified, returns the boolean status of the corresponding object. Otherwise, returns an array of all enabled objects.


load()source abstract public

load( string $name , array $options array() )

Loads a new object onto the collection. Can throw a variety of exceptions

Implementations of this class support a $options['enabled'] flag which enables/disables a loaded object.

Parameters

string $name
Name of object to load.
array $options optional array()
Array of configuration options for the object to be constructed.

Returns

CakeObject
the constructed object

loaded()source public

loaded( string $name null )

Gets the list of loaded objects, or, whether the given object is loaded

Parameters

string $name optional null

Optional. The name of the object to check the status of. If omitted, returns an array of currently-loaded objects

Returns

mixed

If $name is specified, returns the boolean status of the corresponding object. Otherwise, returns an array of all loaded objects.


normalizeObjectArray()source public static

normalizeObjectArray( array $objects )

Normalizes an object array, creates an array that makes lazy loading easier

Parameters

array $objects
Array of child objects to normalize.

Returns

array
Array of normalized objects.

prioritize()source public

prioritize( )

Prioritize list of enabled object

Returns

array
Prioritized list of object

set()source public

set( string $name null , CakeObject $object null )

Adds or overwrites an instantiated object to the collection

Parameters

string $name optional null
Name of the object
CakeObject $object optional null
The object to use

Returns

array
Loaded objects

setPriority()source public

setPriority( string|array $name , integer|null $priority null )

Set priority for an object or array of objects

Parameters

string|array $name

CamelCased name of the object(s) to enable (string or array) If string the second param $priority is used else it should be an associative array with keys as object names and values as priorities to set.

integer|null $priority optional null
Integer priority to set or null for default

trigger()source public

trigger( string|CakeEvent $callback , array $params array() , array $options array() )

Trigger a callback method on every object in the collection. Used to trigger methods on objects in the collection. Will fire the methods in the order they were attached.

Options

  • breakOn Set to the value or values you want the callback propagation to stop on. Can either be a scalar value, or an array of values to break on. Defaults to false.

  • break Set to true to enabled breaking. When a trigger is broken, the last returned value will be returned. If used in combination with collectReturn the collected results will be returned. Defaults to false.

  • collectReturn Set to true to collect the return of each object into an array. This array of return values will be returned from the trigger() call. Defaults to false.

  • modParams Allows each object the callback gets called on to modify the parameters to the next object. Setting modParams to an integer value will allow you to modify the parameter with that index. Any non-null value will modify the parameter index indicated. Defaults to false.

Parameters

string|CakeEvent $callback

Method to fire on all the objects. Its assumed all the objects implement the method you are calling. If an instance of CakeEvent is provided, then then Event name will parsed to get the callback name. This is done by getting the last word after any dot in the event name (eg. Model.afterSave event will trigger the afterSave callback)

array $params optional array()
Array of parameters for the triggered callback.
array $options optional array()
Array of options.

Returns

mixed
Either the last result or all results if collectReturn is on.

Throws

CakeException
when modParams is used with an index that does not exist.

unload()source public

unload( string $name )

Name of the object to remove from the collection

Parameters

string $name
Name of the object to delete.

Properties detail

$_enabledsource

protected array

List of the currently-enabled objects

array()

$_loadedsource

protected array

A hash of loaded objects, indexed by name

array()

$defaultPrioritysource

public integer

Default object priority. A non zero integer.

10

© 2005–2017 The Cake Software Foundation, Inc.
Licensed under the MIT License.
CakePHP is a registered trademark of Cake Software Foundation, Inc.
We are not endorsed by or affiliated with CakePHP.
https://api.cakephp.org/2.10/class-ObjectCollection.html