php - Registering a model as an event listener -
php - Registering a model as an event listener -
i'd know if thought register model class cakeeventlistener within model's php file.
for example, if created model called document implements listener, , @ bottom of document.php there register listener.
class document extends appmodel implements cakeeventlistener { ..... } cakeeventmanager::instance()->attach(classregistery::init('document')); my question nexted calls classregistery::init('document') , if above cause 2 instances of document created.
for example, let's have next in controller.
class documentscontroller extends appcontroller { public function index() { $model = classregistery::init('document'); ..... how many times document instantiated?
classregistery::init('document') controller loads document.php file. wouldn't there sec phone call classregistery::init('document') bottom of document.php befoe first phone call has finished? would somehow strew registry in cakephp?
it shouldn't problem. classregistry::init() instantiates object once. subsequent calls init() homecoming existing object.
i would, however, suggest registering listener in document's __construct function. feels cleaner because aren't mixing self-executing php class file. allows possible injection later on useful unit tests.
php cakephp cakephp-2.3
Comments
Post a Comment