tags:

views:

112

answers:

2

Hi!

If I would need to build up an array with OOP based PHP, would this be the proper way to do it?

class MyClass {

    $array = array();

    function addElement($value) {
        $this->array[] = $value;

    }

    function fetch() {

        $return = $this->memcached->getMulti($this->array);        

        return $return;
    }


}

PHP file where it will be used:

<?php

$this->myClass->addElement('key1');
$this->myClass->addElement('key1');
$this->myClass->addElement('key1');
$var = $this->myClass->fetch();

Thanks a lot

+1  A: 

Take a look at the ArrayAccess interface

erenon
I'm afraid I cant use that, it will make my framework to freak out...
Industrial
Out of interest *why* can't you use it? The `ArrayObject` solution above means you would be.
salathe
+2  A: 

My suggestion: use SPL ArrayObject instead of implementing your own solution.

nuqqsa
It looks clean in the documention, but I am guessing that I cannot get a clean array back from it instead of the array it returns by default?
Industrial
You can get the plain array version of it with ArrayObject::getArrayCopy() http://www.php.net/manual/en/arrayobject.getarraycopy.php
nuqqsa
Fantastic, I know that there was a solution out there!
Industrial