Solved

Collection of objects

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Compiling PHP with Curl plus protocols 8 48
How to get chosen background-color on every line? 10 44
Insert PHP into HTML page. 7 52
Joomla Editing 3 23
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 …
These days socially coordinated efforts have turned into a critical requirement for enterprises.
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

751 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