Note that there are some explanatory texts on larger screens.

plurals
  1. POWierd behaviour using a service manager invokable
    primarykey
    data
    text
    <p>I'm still learning to understand ZF2 and imho the best way is by doing things. I came across this wierd issue and was wondering if this is expected behaviour.</p> <p>In my application i have the following codes</p> <pre class="lang-php prettyprint-override"><code>//In module.php getServiceConfig() return array( 'invokables' =&gt; array( 'hardwareEntity' =&gt; 'Hardware\Model\Hardware', ), } </code></pre> <p>in my controller i retrieve data from a blob of text which results in a array of x elements let's take 3 for the example</p> <pre class="lang-php prettyprint-override"><code>$hardwares = array( 'hw1' =&gt; array( 'name' =&gt; 'router1' 'ip' =&gt; '192.168.0.200', 'type' =&gt; 'router', ), 'hw2' =&gt; array( 'name' =&gt; 'pc1' 'ip' =&gt; '192.168.0.210', 'type' =&gt; 'pc', ), 'hw3' =&gt; array( 'name' =&gt; 'pc2' 'ip' =&gt; '192.168.0.211', 'type' =&gt; 'pc', ), ); </code></pre> <p>i have a hardware class in the hardware module</p> <pre class="lang-php prettyprint-override"><code>namespace Hardware\Model\; class Hardware { protected $name = null; protected $ip = null; protected $type = null; public function exchangeArray(array $data) { $this-&gt;name = (isset($data['name'])) ? $data['name'] : $this-&gt;name; $this-&gt;ip = (isset($data['ip'])) ? $data['ip'] : $this-&gt;ip; $this-&gt;type = (isset($data['type'])) ? $data['type'] : $this-&gt;type; } } </code></pre> <p>Ok the magic comes when i do the following foreach loops i get different results</p> <pre class="lang-php prettyprint-override"><code>foreach($hardwares as $hw) { $h = $this-&gt;getServiceManager()-&gt;get('hardwareEntity'); $h-&gt;exchangeData($hw); $aObjects[] = $h } </code></pre> <p>the $aObjects array now contains 3 elements with objects with the type of Hardware\Model\Hardware, but with the data of the last $hardwares element (aka it overwrites all the classes with the data while looping)</p> <p>RESULT:</p> <pre class="lang-php prettyprint-override"><code>array(3) { [0]=&gt; object(Hardware\Model\Hardware)#219 { ["name":protected]=&gt; string(7) "pc2" ["ip":protected]=&gt; string(13) "192.168.0.211" ["type":protected]=&gt; string(6) "pc" } [1]=&gt; object(Hardware\Model\Hardware)#219 { ["name":protected]=&gt; string(7) "pc2" ["ip":protected]=&gt; string(13) "192.168.0.211" ["type":protected]=&gt; string(6) "pc" } [2]=&gt; object(Hardware\Model\Hardware)#219 { ["name":protected]=&gt; string(7) "pc2" ["ip":protected]=&gt; string(13) "192.168.0.211" ["type":protected]=&gt; string(6) "pc" } </code></pre> <p>But when i do </p> <pre class="lang-php prettyprint-override"><code>foreach($hardwares as $hw) { $h = new \Hardware\Model\Hardware(); $h-&gt;exchangeData($hw); $aObjects[] = $h } </code></pre> <p>it fills the $aObjects array with newly instantiated classes that contain different data each.</p> <p>RESULT:</p> <pre class="lang-php prettyprint-override"><code>array(3) { [0]=&gt; object(Hardware\Model\Hardware)#219 { ["name":protected]=&gt; string(7) "router1" ["ip":protected]=&gt; string(13) "192.168.0.200" ["type":protected]=&gt; string(6) "router" } [1]=&gt; object(Hardware\Model\Hardware)#220 { ["name":protected]=&gt; string(7) "pc1" ["ip":protected]=&gt; string(13) "192.168.0.210" ["type":protected]=&gt; string(6) "pc" } [2]=&gt; object(Hardware\Model\Hardware)#221 { ["name":protected]=&gt; string(7) "pc2" ["ip":protected]=&gt; string(13) "192.168.0.211" ["type":protected]=&gt; string(6) "pc" } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload