Solved

Collection of objects

Posted on 2013-01-18
4
148 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 108

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 108

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
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.

705 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now