<p>I feel like we're missing some code in your examples, but in a <a href="http://buildwithcraft.com/docs/plugins/introduction" rel="nofollow">plugin</a> there is no direct link between a template and its services.  You go through a <a href="http://buildwithcraft.com/docs/plugins/variables" rel="nofollow">Variable</a> class, which is responsible for exposing your <a href="http://buildwithcraft.com/docs/plugins/services" rel="nofollow">service's</a> methods.</p>

<p>So in your example, in your plugin's variables folder, you'd have a <code>MyPluginVariable</code> class that has it's own <code>someFunction</code> method like this:</p>

<pre><code>class MyPluginVariable
{
    public function getMyArray()
    {
        return craft()-&gt;myPlugin-&gt;myArray;
    }

    public function someFunction()
    {
        return craft()-&gt;myPlugin-&gt;someFunction();
    }
}
</code></pre>

<p>Then from your template, assuming you call <code>someFunction()</code> first, you should get the correct output.</p>

<pre><code>{{ craft.myPlugin.someFunction() }}
{{ dump(craft.myPlugin.myArray }}
</code></pre>

<p>I'd also recommend refactoring your service to <a href="https://en.wikipedia.org/wiki/Encapsulation_(computer_programming)" rel="nofollow">encapsulate</a> the public array in a method and use the PHP <code>[]</code> syntax to avoid the function overhead of <code>array_push()</code>.</p>

<p>Something like this:</p>

<pre><code>class MyPluginService extends BaseApplicationComponent
{
    private $_myArray = array('test1', 'test2');

    public function someFunction()
    {
        $someText = "This should end up in myArray";
        $this-&gt;_myArray[] = $someText;
    }

    public function getMyArray()
    {
        return $this-&gt;_myArray;
    }
}
</code></pre>

<p>Then your Variable's <code>getMyArray()</code> method would change to this:</p>

<pre><code>public function getMyArray()
{
    return craft()-&gt;myPlugin-&gt;getMyArray();
}
</code></pre>

CC BY-SA 3.0
2015-11-08T01:06:44.853Z
Thank you Brad (and apologies for the late response). Not only did you elaborate on my own question, but you've answered it perfectly. Extrapolating from you answer, most of my issues are down the syntaxing issues. I think you need to get one of those "buy me a beer" buttons next to your name... you've helped me out various times now. Thanks again.
Mark Notton
CC BY-SA 3.0
2015-11-08T07:39:06.187Z
Hah... next time I'm in the UK, I'll take you up on the beer!
Brad Bell
CC BY-SA 3.0
2015-10-15T00:49:38.060Z
2015-10-15T00:49:38.060Z
Brad Bell
<p>Apologies if this ends up being a silly syntax oversight.</p>

<p>My services file looks like this: </p>

<pre><code>class MyPluginService extends BaseApplicationComponent {
  public $myArray = array('test1', 'test2');

  public function someFunction() {
    $someText = "This should end up in myArray";
    array_push($this-&gt;myArray, $someText);
  }
}
</code></pre>

<p>Assuming the "someFunction" has been called, I later try to dump the array. </p>

<pre><code>{{ dump(craft.myPlugin.myArray) }}
</code></pre>

<p><strong>Expected output:</strong> </p>

<blockquote>
  <p>array(2) { [0]=> string(5) "test1" <a href="https://stackoverflow.com/questions/16136000/correct-array-push-usage-in-a-class">1</a>=> string(5) "test2" [2]=>
  string(29) "This should end up in myArray" }</p>
</blockquote>

<p><strong>Actual output:</strong> </p>

<blockquote>
  <p>array(2) { [0]=> string(5) "test1" <a href="https://stackoverflow.com/questions/16136000/correct-array-push-usage-in-a-class">1</a>=> string(5) "test2" }</p>
</blockquote>

<p><a href="https://stackoverflow.com/questions/16136000/correct-array-push-usage-in-a-class">The solution</a> seems to be pretty unanimous across many PHP related forums. However, this just isn't working in my Craft project. <strong>Have I overlooked something obvious, or is Craft protecting the public array in some way?</strong></p>

<plugin-development><php>
Pushing to an array within a class doesn't work
# Pushing to an array within a class doesn't work

(Asked by: username_0 on 2015-10-15)

Apologies if this ends up being a silly syntax oversight.
My services file looks like this: 
class MyPluginService extends BaseApplicationComponent {
  public $myArray = array('test1', 'test2');

  public function someFunction() {
    $someText = "This should end up in myArray";
    array_push($this->myArray, $someText);
  }
}

Assuming the "someFunction" has been called, I later try to dump the array. 
{{ dump(craft.myPlugin.myArray) }}

Expected output: 

array(2) { [0]=> string(5) "test1" 1=> string(5) "test2" [2]=>
  string(29) "This should end up in myArray" }

Actual output: 

array(2) { [0]=> string(5) "test1" 1=> string(5) "test2" }

The solution seems to be pretty unanimous across many PHP related forums. However, this just isn't working in my Craft project. Have I overlooked something obvious, or is Craft protecting the public array in some way?

**Tags:** plugin-development, php


---

## 1 answers

### Answer by username_1 on 2015-10-15. Score: 2 | Accepted answer

I feel like we're missing some code in your examples, but in a plugin there is no direct link between a template and its services.  You go through a Variable class, which is responsible for exposing your service's methods.
So in your example, in your plugin's variables folder, you'd have a MyPluginVariable class that has it's own someFunction method like this:
class MyPluginVariable
{
    public function getMyArray()
    {
        return craft()->myPlugin->myArray;
    }

    public function someFunction()
    {
        return craft()->myPlugin->someFunction();
    }
}

Then from your template, assuming you call someFunction() first, you should get the correct output.
{{ craft.myPlugin.someFunction() }}
{{ dump(craft.myPlugin.myArray }}

I'd also recommend refactoring your service to encapsulate the public array in a method and use the PHP [] syntax to avoid the function overhead of array_push().
Something like this:
class MyPluginService extends BaseApplicationComponent
{
    private $_myArray = array('test1', 'test2');

    public function someFunction()
    {
        $someText = "This should end up in myArray";
        $this->_myArray[] = $someText;
    }

    public function getMyArray()
    {
        return $this->_myArray;
    }
}

Then your Variable's getMyArray() method would change to this:
public function getMyArray()
{
    return craft()->myPlugin->getMyArray();
}

> **Comments:**
> **username_0**: Thank you Brad (and apologies for the late response). Not only did you elaborate on my own question, but you've answered it perfectly. Extrapolating from you answer, most of my issues are down the syntaxing issues. I think you need to get one of those "buy me a beer" buttons next to your name... you've helped me out various times now. Thanks again.  
> **username_1**: Hah... next time I'm in the UK, I'll take you up on the beer!  

---

