-1

(SO closed out my previous question, so I will try to be clearer this time) When I run the following code in my browser:

<?php
class MyNode {
    public $name;
    public $children = array();
    public $parent;

    public function __construct(string $name, $parent) {
        echo "in constructor\n";
      $this->name = $name;
      echo "name: " . $this->name . ".\n";
      $this->parent = $parent;
      #$this->parent = &$parent;
      echo "parent: " . $this->parent . ".\n";
    }
}
$RootNode = new MyNode("Root", null);
echo "done root x\n";
#$ChildNode = new MyNode("Child1", null);
$ChildNode = new MyNode("Child1", $RootNode);
echo "done child\n";
?>

The browser prints "in constructor name: Root. parent: . done root x in constructor name: Child1." and then stops with the error (in Developer console) "500 (Internal Server Error)". If I use the line (commented above) that passes null instead of $RootNode, it succeeds.

What is the right way to pass $RootNode to the MyNode constructor?

  • PHP does not run in a browser and it's pointless to keep a handle to `parent`, which is being known anyway. – Martin Zeitler Feb 12 '22 at 23:37
  • @MartinZeitler - are you saying the PHP code in my file is being run on the web server? Regardless, I'm interacting with it (badly, in this case) via Chrome. In my simple test case, of course I have reference both to the parent and child via $RootNode and $ChildNode. In a real case, I want to be able to do something like $SomeLeafNode->parent->parent to find the "grandfather node" of $SomeLeafNode. – Kevin Weinrich Feb 14 '22 at 17:03

1 Answers1

1

You're extremely close - if you had error reporting on, the issue would be fairly clear. Your code is emitting an error when it tries to print $this->parent because PHP can't convert instances of MyNode to a string - which PHP needs to do to print it.

Here's an example of your code. It's emitting this error:

Fatal error: Uncaught Error: Object of class MyNode could not be converted to string in /in/T7Os5:12
Stack trace:
#0 /in/T7Os5(18): MyNode->__construct('Child1', Object(MyNode))
#1 {main}
  thrown in /in/T7Os5 on line 12

I suggest implementing a __toString() method (example):

<?php
class MyNode {
    public $name;
    public $children = array();
    public $parent;

    public function __construct(string $name, $parent) {
      echo "in constructor\n";
      $this->name = $name;
      echo "name: " . $this->name . ".\n";
      $this->parent = $parent;
      echo "parent: " . $this->parent . ".\n";
    }
    
    public function __toString()
    {
        return $this->name;
    }
}

$RootNode = new MyNode("Root", null);
echo "done root x\n";
$ChildNode = new MyNode("Child1", $RootNode);
echo "done child\n";
HPierce
  • 7,249
  • 7
  • 33
  • 49
  • My intent is not to print parent, but to create a "link" of sorts to it, so I can link from any given leaf/child node back to its parent, back to its parent, etc. all the way back to the root. I think I'm trying to create a doubly-linked list, which I've done in other languages long ago, but not Python. Thanks very much for the error reporting tip! I definitely need that turned on. – Kevin Weinrich Feb 14 '22 at 17:00
  • I used the PHP error reporting lines you suggested, and now it's clear! As you said, it wasn't the assignment that was the problem, it was when I tried to echo what was no longer a string. Thanks so much @HPierce. – Kevin Weinrich Feb 14 '22 at 22:24