2

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();

2 Answers 2

2

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

Sign up to request clarification or add additional context in comments.

3 Comments

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?
You can get the plain array version of it with ArrayObject::getArrayCopy() php.net/manual/en/arrayobject.getarraycopy.php
Fantastic, I know that there was a solution out there!
1

Take a look at the ArrayAccess interface

1 Comment

Out of interest why can't you use it? The ArrayObject solution above means you would be.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.