Jump to content


Photo

Class that "Pushes" its Instances into an Array


  • Please log in to reply
6 replies to this topic

#1 Zaxnyd

Zaxnyd
  • Members
  • PipPip
  • Member
  • 29 posts

Posted 23 May 2006 - 12:40 AM

I want a class that, upon instantiation, automatically "pushes" the created object into an array.

I have tried numerous ideas with wrapper classes and constructors, but nothing so far has worked. I have a feeling the answer will be painfully obvious once seen, but any assistance would be greatly

This is an example of what doesn't work (disregard any syntactic errors, I freehanded some for simplicity's sake):

class list
{ 
  var $contents = array();
  function add($item)
  {
    array_push($contents, $item);
  }
}

class item extends list
{
  var $name;
  function procedure($name = "")
  {
    $this->name = $name;
    parent::add(this);
  }
}

Thanks in advance for your assistance!

#2 hvle

hvle
  • Members
  • PipPipPip
  • Advanced Member
  • 667 posts
  • Locationmelbourne, Australia

Posted 23 May 2006 - 01:35 AM

I actually take it upon myself to test this idea.
I'm working on something very similar to your question, and I came up with this.

you can add a class into an array, but it's not safe and not recommended.
however, you can serialize it into a string, add them into array, and unserialize it when you need to use.

here are my 2 class. You can not name a class list cuz it's reserve words or function name. so i called it c1

c1 is simply an array class which add whatever object into it's contents
class item's constructor simply serialize and add itself to parent class c1.

try it out and you'll see.

Hope this help.



$test = new item('Tom');

$clone = unserialize($test->contents[0]);

echo $clone->name;

class c1
{
var $contents = array();
function add($item)
{
array_push($this->contents, $item);
}
}

class item extends c1
{
var $name;
function item($name = "")
{
$this->name = $name;
$myobj = serialize($this);
$this->add($myobj);
}
}
Life's too short for arguing.

#3 Zaxnyd

Zaxnyd
  • Members
  • PipPip
  • Member
  • 29 posts

Posted 23 May 2006 - 04:51 PM

Thanks so much for your quick reply; however, I'm still a bit befuddled. [img src=\"style_emoticons/[#EMO_DIR#]/huh.gif\" style=\"vertical-align:middle\" emoid=\":huh:\" border=\"0\" alt=\"huh.gif\" /]

In the example you proposed, how would one access the created list? Shouldn't the "c1" class be instantiated into an object that is referenced within the "item" class?

To clarify my goal in case of any confusion, essentially the end result I am aiming for is to end up with a series of items who are all referenced within an array. One could effectively create each object and manually insert them into an array immediately after instantiation, but for simplicity's sake and to avoid the error of ever forgetting to any, I'd like it to be automatically done within the item's constructor.

I just know I'm overcomplicating this...

Thanks again.

#4 hvle

hvle
  • Members
  • PipPipPip
  • Advanced Member
  • 667 posts
  • Locationmelbourne, Australia

Posted 24 May 2006 - 01:05 AM

in that case, you can have predefined array of objects, and upon construction, add itself to that object array.

There's maybe better way to do this, but this one pop into my mind as now:



    $objectsArray = array();
    $test = new item($objectsArray, 'Tom');
    $clone = unserialize($objectsArray[0]);
    
    echo $clone->name;

    

class item
{
  var $name;
  function item(&$objects, $name)
  {
    $this->name = $name;
    array_push($objects, serialize($this));
  }
}


as you read the code carefully, you'd notice that the objects array passed into item's constructor by reference. In order for this code to work, $objectsArray must be accessable.

Life's too short for arguing.

#5 Zaxnyd

Zaxnyd
  • Members
  • PipPip
  • Member
  • 29 posts

Posted 12 June 2006 - 06:25 PM

I found a better solution:
class itemlist
{
  var $contents = array();
  function add($item){
    array_push($this->contents, $item);
  }
  function newitem($data){
    $temp = new item($data);
    $this->add($temp);
  }
}

class item
{
  var $data;
  function item($data){
    $this->data = $data;    
  }
}

$foo = new itemlist;
$foo->newitem('bar');

var_dump($foo) returns:
object(itemlist)(1) {
  ["contents"]=>
  array(1) {
    [0]=>
    object(item)(1) {
      ["data"]=>
      string(3) "bar"
    }
  }
}


#6 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,017 posts

Posted 12 June 2006 - 08:09 PM

A slight variation which puts the adding to the list in the item constructor as preferred by the OP

class  collection {
    private $items = array();
    
    function add($item) {
        $this->items[] = $item;
    }
    
    function __toString() {
        foreach($this->items as $itm) {
            $str .= $itm->value . '<br>';
        }
        return $str;
    }
}

class item {
    private $value;
    
    function __construct($value, $collection) {
        $this->value = $value;
        $collection->add($this);
    }
    
    function __get($var) {
        return $this->$var;
    }
    
}

$mycoll  = new collection;

$item1 = new item('foo', $mycoll);
$item2 = new item('bar', $mycoll);
$item3 = new item('Hello', $mycoll);
$item4 = new item('World', $mycoll);

echo $mycoll;

If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#7 Zaxnyd

Zaxnyd
  • Members
  • PipPip
  • Member
  • 29 posts

Posted 16 June 2006 - 09:40 PM

That's exactly the solution I was looking for. Brilliant! Thank you!




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users