<p>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:</p>

<pre><code>craft()-&gt;on('capture_submissions.onBeforePost', array($this, 'onCaptureListener'));

public function onCaptureListener(Event $event) {
 ...
}
</code></pre>

CC BY-SA 3.0
2016-05-25T16:37:53.217Z
2016-05-25T16:37:53.217Z
Alexander Holsgrove
<p>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 <code>init()</code> looks like this:</p>

<pre><code>public function init() {

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

<p>When submitting the form, I get the following error:</p>

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

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

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

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

<plugin-development><events>
Plugin using event listener throws error using $this
# Plugin using event listener throws error using $this

(Asked by: username_0 on 2016-05-25)

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?

**Tags:** plugin-development, events

---

## 1 answers

### Answer by username_0 on 2016-05-25. Score: 3 | Accepted answer

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) {
 ...
}


---

