Hi,

I'm trying to build a tree of objects in PHP, and having all sorts of
trouble. Ideally, I'd like to end up with a reference to an object, 
which has an array of (references to) child-objects. Each child-object,
except the root, also contains a reference to its parent. A sample :

<?php

class myObj {

  var $children;
  var $parent;
  var $name;

  function myObj($name) {

    $this->name = $name;
    $this->children = array();

  }

}

session_start();

if(!$_SESSION['root']) {

  $root = new myObj("root");
  $child1 =& new myObj("child1");
  $child1->parent =& $root;
  $root->children[] =& $child1;
  $child2 =& new myObj("child2");
  $child2->parent =& $child1;
  $child1->children[] =& $child2;

  $_SESSION['root'] =& $root;

}

?>

Load this to create the session, reload it to (try to) use the session.
This code works on Linux (php 4.2.1), and segfaults on OpenBSD (also
php 4.2.1) with one or more of these when reloaded :

[Thu Jun  6 22:24:31 2002] [notice] child pid 12335 exit signal 
    Segmentation fault (11)
- or -
FATAL:  erealloc():  Unable to allocate -541078048 bytes
- or -
[Thu Jun  6 22:25:00 2002] [notice] child pid 23244 exit signal 
    Illegal instruction (4)

After some searching, I found a bug report stating that circular references 
are not supported, and probably won't be for some time to come. 
What I'd like to know is :
 - are circular refs the problem here, and if so, why does it seem
   to work on linux ?
 - could somebody point me to a better way of building an n-ary tree of
   objects that can be stored in sessions ? I tried searching, but found
   little usefull code so far.
   in the app I'm trying to get started, each object could be one of a
   set of classes, all derived from the same base-class, which should
   hold all tree-manipulation methods. something like the class DomNode,
   but more extensible (in the regular PHP-sense of 'extends').

-- 
CUL8R, Peter.


-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to