Cannot redeclare class JDispatcher

936 views
Skip to first unread message

WP4J

unread,
Jul 10, 2013, 2:29:07 PM7/10/13
to joomla-de...@googlegroups.com

I tried to do the following from within a module:

How To Trigger Events

This leads us to the meat of this article: how to trigger events so implementing plug-ins can act on them. The first thing you need to do is to load your plug-in group. This is done via the following code:

JPluginHelper::importPlugin( 'myplugingroup' );

This will load all enabled plug-ins that have defined themselves as part of your group. The next thing you need to do is get an instance of the JEventDispatcher class:

$dispatcher = JEventDispatcher::getInstance();

Notice two things here. First, we are using the getInstance() method, not new, to create a new instance. That is because we need to get the global singleton instance of the JEventDispatcher object that contains a list of all the plug-ins available.

Next, we need to trigger our custom event:

$results = $dispatcher->trigger( 'onCdAddedToLibrary', array( &$artist, &$title ) );
Here we have triggered the event onCdAddedToLibrary and passed in the artist name and title of the track. Which and how many parameters you pass is up to you. Passing parameters by reference (using an &) allows the plug-ins to change the variables passed in. All plug-ins will receive these parameters, process them and optionally pass back information. The calling code can access the returned values via the array $results (each element corresponds to the result of one plugin). 

-------------------

It does not work, I get an error:

<br /><b>Fatal error</b>: Cannot redeclare class JDispatcher in <b>/libraries/joomla/event/dispatcher.php</b> on line <b>279</b><br />

Yet when I do this:

$dispatcher = JDispatcher::getInstance();

It works fine - note that I am only changing JEventDispatcher to JDispatcher

Is it ok to use plain old JDispatcher?

Currently using J! 2.5 but will be supporting 3.0+ in the module - will I have problems?

Thanks!



Donald Gilbert

unread,
Jul 10, 2013, 2:40:37 PM7/10/13
to joomla-de...@googlegroups.com
If you going to be supporting 2.5.x and 3.0+, then you should use JDispatcher, even though it's deprecated. JDispatcher is still present in 3.x, but it's in the legacy folder, so it's ok to use. Alternatively, you could define your own proxy class, but there really is no need, as JDispatcher _is_ the proxy class for 3.x and the actual class in 2.5.





--
You received this message because you are subscribed to the Google Groups "Joomla! General Development" group.
To unsubscribe from this group and stop receiving emails from it, send an email to joomla-dev-gene...@googlegroups.com.
To post to this group, send an email to joomla-de...@googlegroups.com.
Visit this group at http://groups.google.com/group/joomla-dev-general.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Denis Ryabov

unread,
Jul 10, 2013, 3:04:56 PM7/10/13
to joomla-de...@googlegroups.com
I think it's better to call
JFactory::getApplication()->triggerEvent( 'onCdAddedToLibrary', array( &$artist, &$title ) );
instead of direct work with JDispatcher.
 
 
10.07.2013, 22:41, "Donald Gilbert" <dilber...@gmail.com>:
--
С уважением,
Денис Рябов

Donald Gilbert

unread,
Jul 10, 2013, 3:22:41 PM7/10/13
to joomla-de...@googlegroups.com
Working directly with JDispatcher is perfectly fine. IMO, it's actually better than using the triggerEvent method of JApplication.

Why is it better? Because in reality, your code is needing to interact with the dispatcher object, not the application object. By using JFactory::getApplication(), your code now depends on JFactory, JApplication, as well as JDispatcher. If something changes in any of those classes, you have to make sure your code doesn't break.

Instead, if you directly use JDispatcher, then your code only depends on that single class, and you only have to check your code if that class changes. Less dependencies === less long term maintenance. 

Denis Ryabov

unread,
Jul 10, 2013, 3:30:43 PM7/10/13
to joomla-de...@googlegroups.com
The problem is that it is unclear how it will work in next Joomla releases. For example, in Joomla!3.x you can change application's dispatcher object by call to JFactory::getApplication()->loadDispatcher($newDispatcher), but JPluginHelper::importPlugin() is still based on hardcoded JEventDispatcher::getInstance() (and JDispatcher::getInstance() is deprecated).
 
10.07.2013, 23:23, "Donald Gilbert" <dilber...@gmail.com>:

WP4J

unread,
Jul 10, 2013, 3:34:18 PM7/10/13
to joomla-de...@googlegroups.com
So I take it I will be ok doing it like this for the time being - seems strange that the docs state to use JEventDispatcher when it causes an error, also seems strange that using a getInstance() should cause this as I thought these were autoloader, factory type methods...

Thanks for the advice guys!

Matt Thomas

unread,
Jul 10, 2013, 3:38:50 PM7/10/13
to Joomla! General Development
On Wed, Jul 10, 2013 at 3:34 PM, WP4J <wp4j...@gmail.com> wrote:
 seems strange that the docs state to use JEventDispatcher when it causes an error

Is this possibly a typo or something carried over from an older version of Joomla?

Best,

Matt Thomas
Founder betweenbrain
Phone: 203.632.9322
Twitter: @betweenbrain

Donald Gilbert

unread,
Jul 10, 2013, 3:47:26 PM7/10/13
to joomla-de...@googlegroups.com
It's not a typo, those are the docs for 3.x, where you should use JEventDispatcher if you are only needing to support 3.x. However, since that class does not exist in 2.5, but JDispatcher does (and it exists in 3.x), if you need to support both, it's easiest just to use JDispatcher.


--

Donald Gilbert

unread,
Jul 10, 2013, 3:49:21 PM7/10/13
to joomla-de...@googlegroups.com
JDispatcher::getInstance() doesn't exist in 3.x, so it can't be deprecated. The whole JDispatcher class is deprecated, but the class is merely a proxy to JEventDispatcher. Everything else is identical.

I'm not sure why you would want to or why it's possible to change the dispatcher in 3.x.

Matt Thomas

unread,
Jul 10, 2013, 3:50:30 PM7/10/13
to Joomla! General Development
Thanks for clarifying that Don. The document states This article is for Joomla! CMS Version(s) Joomla 2.5 Joomla 3.x. I'll see if I can clarify that in the document at some point.




Best,

Matt Thomas
Founder betweenbrain
Phone: 203.632.9322
Twitter: @betweenbrain



Donald Gilbert

unread,
Jul 10, 2013, 3:51:58 PM7/10/13
to joomla-de...@googlegroups.com
Oh - I didn't even realize it said that; guess I should have looked first. :P

You could add a note at the top of the page that says something along the lines of "everything here applies to 2.5 and 3.x, but if you're using 2.5, then change JEventDispatcher to JDispatcher

Matt Thomas

unread,
Jul 10, 2013, 3:57:00 PM7/10/13
to Joomla! General Development
Done ;-)

Best,

Matt Thomas
Founder betweenbrain
Phone: 203.632.9322
Twitter: @betweenbrain



Donald Gilbert

unread,
Jul 10, 2013, 4:02:10 PM7/10/13
to joomla-de...@googlegroups.com
The reason it actually triggers the error is because of the autoloader. JDispatcher gets loaded early on in the application boot process. When you try to load JEventDispatcher in 2.5, the autoloader looks for a file at libraries/joomla/event/dispatcher.php. It then includes it, which triggers the error, because it's re-declaring JDispatcher.


Donald Gilbert

unread,
Jul 10, 2013, 4:06:48 PM7/10/13
to joomla-de...@googlegroups.com
Also, for those curious about the link: http://docs.joomla.org/Supporting_plugins_in_your_component

Took me a while to find it.
Reply all
Reply to author
Forward
0 new messages