<p>Matrix initializes its blocks’ field types multiple times – once for each existing block in the field, and one last time to get the initial input HTML that should be used when new blocks are added.</p>

<p>For existing blocks, <code>$this-&gt;element</code> will be set on the field types, referring back to the existing MatrixBlockModel objects representing each of those blocks.</p>

<p>That won’t be the case the time that your field type is just telling Matrix what the initial input HTML should be, however, since no corresponding Matrix block exists yet.</p>

<p>When you call this:</p>



<pre class="lang-php prettyprint-override"><code>var_dump($this-&gt;element);
</code></pre>

<p>it’s not giving you any errors because <code>$this-&gt;element</code> will just be <code>null</code> when there is no element set, and there’s nothing wrong with dumping a <code>null</code> variable.</p>

<p>But when you call this;</p>

<pre class="lang-php prettyprint-override"><code>var_dump($this-&gt;element-&gt;getOwner())
</code></pre>

<p>now you’re trying to access a method on <code>$this-&gt;element</code>, which won’t necessarily be set, and that’s when you’re getting that exception.</p>

<p>So what you need to do is ensure that <code>$this-&gt;element</code> is actually set before trying to access <code>getOwner()</code>:</p>

<pre class="lang-php prettyprint-override"><code>if (isset($this-&gt;element))
{
    $owner = $this-&gt;element-&gt;getOwner();

    if ($owner)
    {
        // ...
    }
}
</code></pre>

<p>Note that I’m also not assuming that <code>getOwner()</code> will actually return anything, because that’s not a certainty either. For example if you create/save a new entry but there are validation errors, when the Edit Entry page reloads, you will have MatrixBlockModel objects for each of the blocks you had created, which <code>$this-&gt;element</code> will point to, but they won’t actually have IDs nor an owner yet.</p>

CC BY-SA 3.0
2014-08-06T16:29:09.630Z
I resolved putting my code inside a if(is_object($this->element))
Your suggestion, anyway, has been very valuable. Thank you.
user3748541
CC BY-SA 3.0
2014-08-06T18:47:01.537Z
Yeah that will work for the most part, but as noted above, even if `$element` is set, `getOwner()` won’t necessarily return anything.
Brandon Kelly
CC BY-SA 3.0
2014-08-07T09:27:18.363Z
You are right, but,anyway, now I can better check errors, putting everything inside a try/catch. Consider that "call to a member function of a non object" is a non catchable error (there is an rfc: https://wiki.php.net/rfc/catchable-call-to-member-of-non-object)
user3748541
CC BY-SA 3.0
2014-08-07T10:04:31.053Z
In this case it’s catchable - the exception is being thrown by Yii’s \CComponent class. You just need to remember that \CComponent is in the root namespace, whereas your code is in the Craft namespace, so you need to remember to type the “\” in `catch (\Exception $e)`.
Brandon Kelly
CC BY-SA 3.0
2014-08-06T11:02:29.700Z
2014-08-06T11:02:29.700Z
Brandon Kelly
<p>I'm having some trouble trying to write a plugin. I'm trying to create a simple fieldtype that I'm going to use inside a matrix block.
So far the public function <code>getInputHtml</code> of my fieldtype class, contains only:</p>

<pre><code>var_dump($this-&gt;element)
</code></pre>

<p>And it shows me, as expected:</p>

<pre><code>object(Craft\MatrixBlockModel)#1121 (25){["elementType":protected]=&amp;gt; string(11) "MatrixBlock" [.......]
</code></pre>

<p>The problem is that, as I try to change the previous</p>

<pre><code>var_dump($this-&gt;element)
</code></pre>

<p>with a</p>

<pre><code>var_dump($this-&gt;element-&gt;getOwner())
</code></pre>

<p>(or any other method/property of the MatrixBlockModel class), I obtain:</p>

<blockquote>
  <p>"Call to a member function getOwner() on a non-object"</p>
</blockquote>

<p>Any suggestion? Could it have something to do with the magic methods (maybe __call) inherited by CComponent class?</p>

CC BY-SA 3.0
2014-08-05T16:42:07.403Z
If you setup xDebug (http://xdebug.org/) and force step into the $this->element->getOwner() line, you'll be able to see if any magic methods are being called on it.
Brad Bell
<plugin-development><matrix>
MatrixBlockModel error - "Call to a member function on a non-object"
# MatrixBlockModel error - "Call to a member function on a non-object"

(Asked by: username_0 on 2014-08-05)

I'm having some trouble trying to write a plugin. I'm trying to create a simple fieldtype that I'm going to use inside a matrix block.
So far the public function getInputHtml of my fieldtype class, contains only:
var_dump($this->element)

And it shows me, as expected:
object(Craft\MatrixBlockModel)#1121 (25){["elementType":protected]=> string(11) "MatrixBlock" [.......]

The problem is that, as I try to change the previous
var_dump($this->element)

with a
var_dump($this->element->getOwner())

(or any other method/property of the MatrixBlockModel class), I obtain:

"Call to a member function getOwner() on a non-object"

Any suggestion? Could it have something to do with the magic methods (maybe __call) inherited by CComponent class?

**Tags:** plugin-development, matrix
> **Comments:**
> **username_1**: If you setup xDebug (http://xdebug.org/) and force step into the $this->element->getOwner() line, you'll be able to see if any magic methods are being called on it.  

---

## 1 answers

### Answer by username_2 on 2014-08-06. Score: 2 | Accepted answer

Matrix initializes its blocks’ field types multiple times – once for each existing block in the field, and one last time to get the initial input HTML that should be used when new blocks are added.
For existing blocks, $this->element will be set on the field types, referring back to the existing MatrixBlockModel objects representing each of those blocks.
That won’t be the case the time that your field type is just telling Matrix what the initial input HTML should be, however, since no corresponding Matrix block exists yet.
When you call this:
var_dump($this->element);

it’s not giving you any errors because $this->element will just be null when there is no element set, and there’s nothing wrong with dumping a null variable.
But when you call this;
var_dump($this->element->getOwner())

now you’re trying to access a method on $this->element, which won’t necessarily be set, and that’s when you’re getting that exception.
So what you need to do is ensure that $this->element is actually set before trying to access getOwner():
if (isset($this->element))
{
    $owner = $this->element->getOwner();

    if ($owner)
    {
        // ...
    }
}

Note that I’m also not assuming that getOwner() will actually return anything, because that’s not a certainty either. For example if you create/save a new entry but there are validation errors, when the Edit Entry page reloads, you will have MatrixBlockModel objects for each of the blocks you had created, which $this->element will point to, but they won’t actually have IDs nor an owner yet.

> **Comments:**
> **username_0**: I resolved putting my code inside a if(is_object($this->element)) Your suggestion, anyway, has been very valuable. Thank you.  
> **username_2**: Yeah that will work for the most part, but as noted above, even if `$element` is set, `getOwner()` won’t necessarily return anything.  
> **username_0**: You are right, but,anyway, now I can better check errors, putting everything inside a try/catch. Consider that "call to a member function of a non object" is a non catchable error (there is an rfc: https://wiki.php.net/rfc/catchable-call-to-member-of-non-object)  
> **username_2**: In this case it’s catchable - the exception is being thrown by Yii’s \CComponent class. You just need to remember that \CComponent is in the root namespace, whereas your code is in the Craft namespace, so you need to remember to type the “\” in `catch (\Exception $e)`.  

---

