简体   繁体   中英

I don't understand where beginChildren, endChildren functions are called

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

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

" 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM