2

I have written a plugin to hook into an event raised by a contact form plugin. I am running PHP 5.6 locally, and the plugin works just fine. Moving the code to a server running 5.3 (which I cannot change) leads to an error. My init() looks like this:

public function init() {

   /* Intercepts the Capture form submission */
   craft()->on('capture_submissions.onBeforePost', function(Event $event) {
      $data = array(
         'email_address'   => $capture_data['email'],
      );
      $result = $this->addMember($data);
}

When submitting the form, I get the following error:

Fatal error: Using $this when not in object context in /home/mysite/craft/plugins/myplugin/MyPlugin.php on line 69

By changing the plugin to use MyPlugin:: instead of $this-> leads to an error in Craft core:

Fatal error: Using $this when not in object context in /home/mysite/craft/app/etc/components/BaseSavableComponentType.php on line 41

What is the correct way to call my plugin methods when running as an event listener?

3

Seem to be doing well at asking and then answering my own questions. If anyone has the same issue as me, it is because you cannot use an anonymous function in the callback. With my example above, the correct way is thus:

craft()->on('capture_submissions.onBeforePost', array($this, 'onCaptureListener'));

public function onCaptureListener(Event $event) {
 ...
}
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.