Industrial
Industrial

Reputation: 42788

PHP oop build array

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

Upvotes: 2

Views: 2561

Answers (2)

nuqqsa
nuqqsa

Reputation: 4521

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

Upvotes: 2

erenon
erenon

Reputation: 19118

Take a look at the ArrayAccess interface

Upvotes: 1

Related Questions