Jump to content

Archived

This topic is now archived and is closed to further replies.

Zaxnyd

Class that "Pushes" its Instances into an Array

Recommended Posts

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):

[code]
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);
  }
}
[/code]

Thanks in advance for your assistance!

Share this post


Link to post
Share on other sites
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);
}
}

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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:


[code]

    $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));
  }
}

[/code]

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.

Share this post


Link to post
Share on other sites
I found a better solution:
[code]
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');

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

Share this post


Link to post
Share on other sites
A slight variation which puts the adding to the list in the item constructor as preferred by the OP

[code]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;[/code]

Share this post


Link to post
Share on other sites
That's exactly the solution I was looking for. Brilliant! Thank you!

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.