1

I'm a novice, trying to learn PHP (and object-oriented programming in general). I've tried to follow the threads, but I can't see where the functions current(), beginChildren() and endChildren() are getting called. Many thanks in advance if anyone can let me know where these get called, and equally importantly, how to I search this out by myself next time?

class TableRows extends RecursiveIteratorIterator {
    function __construct($it) {
        parent::__construct($it, self::LEAVES_ONLY);
    }

    function current() {
        return "<td style='width:150px;border:1px solid black;'>" . parent::current(). "</td>";
    }

    function beginChildren() {
        echo "<tr>";
    }

    function endChildren() {
        echo "</tr>" . "\n";
    }

}

$servername = "localhost";
$username = "root";
$password = "";
$dbname = "testDBPDO";

try {
    $conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    $stmt = $conn->prepare("SELECT id, firstname, lastname FROM MyGuests");
    $stmt->execute();

    // set the resulting array to associative
    $result = $stmt->setFetchMode(PDO::FETCH_ASSOC);
    foreach(new TableRows(new RecursiveArrayIterator($stmt->fetchAll()))    as $k=>$v) {
        echo $v;
    }
}
Crowdpleasr
  • 3,574
  • 4
  • 21
  • 37
  • you need to call them inside you `foreach` to render data in table format if you want. – urfusion Aug 18 '18 at 06:00
  • Many thanks, but I still don't understand. Those functions already seem to be getting called "behind the scenes" because the code above works beautifully as is (produces a well-formatted table), but when I comment out those three functions, I lose the formatting. So something is already calling those functions somewhere, I just can't see where they're getting called. – Crowdpleasr Aug 18 '18 at 06:11
  • Object is create `new TableRows(new RecursiveArrayIterator` and constuctor call `parent::__construct($it, self::LEAVES_ONLY);` called the funtions. – urfusion Aug 18 '18 at 06:13
  • 1
    Thank you. But I still don't see how the parent constructor is calling those functions. I looked in RecursiveIteratorIterator and RecursiveArrayIterator, but those looked like Interfaces and I didn't see any implementation of the Constructor (or the other functions). public RecursiveIteratorIterator::__construct ( Traversable $iterator [, int $mode = RecursiveIteratorIterator::LEAVES_ONLY [, int $flags = 0 ]] ) – Crowdpleasr Aug 18 '18 at 06:20

2 Answers2

1

in answer to " how to I search this out by myself next time? "
I saw your 'TableRows' class extends from 'RecursiveIteratorIterator', so I have googled 'RecursiveIteratorIterator'

then I have found the refernce of it : PHP: RecursiveIteratorIterator - Manual
there I have found some usefule information about your issue

another useful link in stackoverflow : How does RecursiveIteratorIterator work in PHP?


have a good learning time

Ali
  • 626
  • 1
  • 11
  • 29
0

"beginChildren" in this case is a function that acts within a nested list, to show in this specific case an html element. I am referring to a nested list because the HTML element that represents a list within a table is opened with 'tr' to show the current that it is inside a cell 'td' and then closes with the 'endChildren' and its respective /tr. It is a function that acts as a complement to nested lists; very used in HTML that acts a lot with markers.

MiSCapu
  • 13
  • 5