1 回答

TA貢獻(xiàn)1820條經(jīng)驗(yàn) 獲得超3個(gè)贊
正如您已經(jīng)說(shuō)過(guò)的,new self()將實(shí)例化一個(gè)新的。用于$this將其引用到對(duì)象本身:
class BookXML extends \DOMDocument
{
? ? public function filterByYear(int $year)
? ? {
? ??
? ? ? ? $books = [];
? ? ? ? $document = $this; // $this not new self()
? ? ? ? $xpath = new DOMXPath($document);
? ? ? ? $booksObjs = $document->documentElement;
? ? ? ? $query = 'string(year)';
? ? ? ? foreach ($booksObjs->childNodes as $booksObj) {
? ? ? ? ? ? $yearxml = $xpath->evaluate($query, $booksObj);
? ? ? ? ? ??
? ? ? ? ? ? if ($yearxml == $year) {
? ? ? ? ? ? ? ? $books[] = $booksObj;
? ? ? ? ? ? }
? ? ? ? }
? ? ? ? return $books;
? ? }
}
- 1 回答
- 0 關(guān)注
- 174 瀏覽
添加回答
舉報(bào)