1

How can I call methods from an array of objects (that hold an array of objects). I read: Get array with results of object method on each item in an array of objects in PHP but could not get it.

Here is my testcode: the first object holds attributes, then an object holds a record of the multiple attributes.

/*--------------------------------- */
class SqliteAttribute {

    private $_fieldname = '';
    private $_fieldvalue = '';
    private $_type = 'TEXT';
    private $_key = true;

    function __construct($fieldname, $fieldvalue, $text, $key) {
        $this->_fieldname  = $fieldname;
        $this->_fieldvalue = $fieldvalue;
        $this->_text       = $text;
        $this->_key        = $key;
    }

    function AsArray() {
        $tempArray = array('fieldname'     => $this->_fieldname,
                           'fieldvalue'    => $this->_fieldvalue,
                           'type'          => $this->_type,
                           'key'           => $this->_key
        );
        return $tempArray;
    }
}

/*--------------------------------- */
class SqliteRecord {

    private $_attributes = array();

    function __construct() {
    }

    function AddAttribute($fieldname, $fieldvalue, $text, $key) {
        $attribute          = new SqliteAttribute($fieldname, $fieldvalue, $text, $key);
        $this->attributes[] = $attribute;
        var_dump($this->_attributes); // shows it!
    }

    function AsArray() {
        $temp_array = array();
        var_dump($this->_attributes); // shows nothing
        foreach ($this->_attributes as $key => $value) {
            $temp_array[] = $value->AsArray();
        }
        return $temp_array;
    }

}

And I call it like this

function updateFiles($files, $rootpath) {
    $recordset = new SqliteRecordSet;
    foreach ($files as $file) {
        $record = new SqliteRecord;
        $record->AddAttribute('Path', $file[0], 'TEXT', true);

        print_r($record->AsArray()); // shows nothing       
    }

    $recordset->insertIfNotExist_index();
}

1 Answer 1

1

$this->attributes vs $this->_attributes

you should always develop code with error reporting set to E_ALL and display_errors on. php would have notified you of your mistake here.

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

3 Comments

argh you are right argh argh argh (just to note that i spend some hours on this searching in wrong directions)
p.s. I have error_reporting(E_ALL); ini_set('display_errors', '1'); but did not show
either it never got set, or you have other code that unset it, or you have mucked with php's error handler.

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.