Solved

Collection of objects

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
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…

863 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

21 Experts available now in Live!

Get 1:1 Help Now