PHP 中的方法链接时调用不同的进程?
首先,为我的英语缺陷和愚蠢感到抱歉:O
我只想知道在方法链接中实现这一点的任何程序。根据 DOMDocument 类,如果我们执行方法链接,则字符序列会相应更改,例如:
$dom = new DOMDocument();
$applicant = $dom->createElement('applicant');
$name = $dom->createElement('name');
$firstname = $dom->createElement('firstname', 'Joe');
$middlename = $dom->createElement('middlename', 'Johnson');
$lastname = $dom->createElement('lastname', 'Smith');
$dom->appendChild($applicant);
$applicant->appendChild($name);
$name->appendChild($firstname);
$name->appendChild($middlename);
$name->appendChild($lastname);
//OUTPUT
<applicant>
<name>
<firstname>Joe</firstname>
<middlename>Johnson</middlename>
<lastname>Smith</lastname>
</name>
</applicant>
如果我这样做
$dom->appendChild($applicant);
$applicant->appendChild($name);
$name->appendChild($firstname)
->appendChild($middlename);
$name->appendChild($lastname);
//OUTPUT
<applicant>
<name>
<firstname>
Joe
<middlename>Johnson</middlename>
</firstname>
<lastname>Smith</lastname>
</name>
</applicant>
-- 怎么样?
Firstly, sorry for my flawed english and being dumb :O
I just want to know any procedure to make this possible in method chaining. As per the DOMDocument class, the sequence of the characters are altered accordingly if we perform method chaining for example:
$dom = new DOMDocument();
$applicant = $dom->createElement('applicant');
$name = $dom->createElement('name');
$firstname = $dom->createElement('firstname', 'Joe');
$middlename = $dom->createElement('middlename', 'Johnson');
$lastname = $dom->createElement('lastname', 'Smith');
$dom->appendChild($applicant);
$applicant->appendChild($name);
$name->appendChild($firstname);
$name->appendChild($middlename);
$name->appendChild($lastname);
//OUTPUT
<applicant>
<name>
<firstname>Joe</firstname>
<middlename>Johnson</middlename>
<lastname>Smith</lastname>
</name>
</applicant>
If I do
$dom->appendChild($applicant);
$applicant->appendChild($name);
$name->appendChild($firstname)
->appendChild($middlename);
$name->appendChild($lastname);
//OUTPUT
<applicant>
<name>
<firstname>
Joe
<middlename>Johnson</middlename>
</firstname>
<lastname>Smith</lastname>
</name>
</applicant>
--
How is that?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
appendChild
返回附加的子项,因此您的下一个appendChild
会作用于该返回值。因此,在第一个示例中,您总是在第二个示例中使用
$name
,两次使用$name
,一次使用$firstname
。请参阅: http://www.php.net/manual/en/domnode.appendchild .php
appendChild
returns the appended child, so your nextappendChild
acts on that return value.So in your first example you're allways working on
$name
in the second twice on$name
and once on$firstname
.See: http://www.php.net/manual/en/domnode.appendchild.php
不太确定你的意思。
无论如何,正如您可能知道的那样,链接是这样完成的:
如果您需要一个返回另一种对象的方法,那么它是这样完成的:
当然,您可以像这样返回另一个对象的另一个实例(
appendChild
方法):Not quite sure what you meant.
Anyway chaining as you probabily know it's done like this:
If you need a methods that returns another kind of object it's done like this:
Of course you can return another instance of another object like this (what does
appendChild
methods):