Solved

Collection of objects

Posted on 2013-01-18
4
156 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 110

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 110

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

Want Experts Exchange at your fingertips?

With Experts Exchange’s latest app release, you can now experience our most recent features, updates, and the same community interface while on-the-go. Download our latest app release at the Android or Apple stores today!

Question has a verified solution.

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

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this. Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it i…
These days socially coordinated efforts have turned into a critical requirement for enterprises.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to dynamically set the form action using jQuery.

623 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