0

I have a heard time figuring out how to use a class as an array. This response helped me a bit on the way; but it doesn't work.

I have a class like this one:

class body_text
{
    // class body_text's attributes
    var $position;
    var $content;
}

And I want to use it as an array:

$body_text_id = 0; 

while ($row['page'] <> "")
{           
    $body_text[$body_text_id] -> position = trim($row["page"]);
    $body_text[$body_text_id] -> content = trim($row["text");
    $body_text_id++;
}

Now, it seems I would need to use this for my class

class body_text_class extends ArrayObject

and use this then:

$body_text = new body_text_class(); 

But that doesn't work.

I am sure there is a simple answer to this... but I can't find it. Thanks for any hints!

EDIT: Added another line in the loop to make it clearer. Sorry! My aim: I get back many bits of text and its position within a website from a first query ("$row['page']"), and I want to store these in a variable for later use.

3
  • 3
    Why do you want to do this? Maybe it helps to explain your use case. Commented Jan 13, 2014 at 16:46
  • The short answer is that your class will have to implement the ArrayAccess interface. The long answer is that it's unclear what exactly you're trying to do here, so a precise answer is not possible. Commented Jan 13, 2014 at 16:48
  • I edited my entry. Perhaps it's easier to understand now. Thanks for your help! Commented Jan 14, 2014 at 6:28

1 Answer 1

2

Hard to follow the code but you are just showing an array of objects:

for($i=0; $i<10; $i++) {
    $body_text[$i] = new body_text;
    $body_text[$i]->position = 'something';
}
print_r($body_text);
Sign up to request clarification or add additional context in comments.

Comments

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.