Solved

Collection of objects

Posted on 2013-01-18
4
152 Views
Last Modified: 2013-02-02
Im still learning at OOP for PHP, and its been suggested that I use an object instead of an array, my question is how can I create a collection of objects, and then pull them all back to go through them in order.

For example, I've created an object and a constructor to set the variables:-
class menuElement{
    public $menuText;
    pulic $onClick;
    function menuElement($menuText,$onClick) {
      $this->menuText = $menuText;
      $this->onClick = $onClick;
    }
}

Open in new window


What I need to do is create 6+ instances of my menuElement object and then pull the object from the first to the last.

I don't have a clue where to start on this, apart from to create a collection of objects I need to use an Iterator but cant make sense of it.
0
Comment
Question by:tonelm54
  • 2
  • 2
4 Comments
 
LVL 109

Assisted Solution

by:Ray Paseur
Ray Paseur earned 250 total points
ID: 38793678
You want to get in the habit of naming the constructor __construct() instead of relying on the PHP4 notation that uses a name coincident with the class name.

For something like this, I would create an array of objects.  You could also create an object full of objects, but I'm not sure that would confer any advantage.

<?php // RAY_temp_tonelm54.php
error_reporting(E_ALL);
echo '<pre>';


// DEMONSTRATE AN ARRAY OF OBJECTS


Class Thing
{
    public function __construct($x)
    {
        $this->data = $x;
    }
}

// SOME DATA TO ASSIGN TO OUR OBJECTS
$ltr = range('A', 'D');
print_r($ltr);

// A PLACE TO KEEP OUR OBJECTS
$arr = array();

// MAKE THE ARRAY OF OBJECTS
foreach ($ltr as $uni)
{
    $arr[] = new Thing($uni);
}
var_dump($arr);

Open in new window

0
 
LVL 27

Accepted Solution

by:
Lukasz Chmielewski earned 250 total points
ID: 38794118
Or, if you're into OOP, you can create an object with array of objects, like:

<?php
error_reporting(E_ALL);

class menuElement{
    public $menuText;
    public $onClick;
    function menuElement($menuText,$onClick) {
      $this->menuText = $menuText;
      $this->onClick = $onClick;
    }
}

class Something {
        
    public $myobjects = array();
    public function addSomething(menuElement $mo) {
        $this->myobjects[] = $mo;
        return $this;
    }
    
}


$something = new Something();
$something->addSomething(new menuElement(1,1))
          ->addSomething(new menuElement(2,2))
          ->addSomething(new menuElement(3,3))
          ->addSomething(new menuElement(4,4))
          ->addSomething(new menuElement(5,5));
echo '<pre>';
print_r($something);

Open in new window

0
 
LVL 109

Expert Comment

by:Ray Paseur
ID: 38794153
@Roads_Roads: I thought about that, but it felt like using a bucket to carry a bucket full of buckets.

:)

Best to all, ~Ray
0
 
LVL 27

Expert Comment

by:Lukasz Chmielewski
ID: 38794190
Yeah, you may say that :P
I thought it would be a good practice for OOP anyway...
0

Featured Post

Are your AD admin tools letting you down?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question