On 06/01/2017 14:03, Willian Veiga wrote:
Hello,

What do you think about a new method, called atXPath, that would return a
single SimpleXMLElement?

Details:

https://github.com/php/php-src/pull/1717
https://bugs.php.net/bug.php?id=45201

Thank you, Willian.


My first thought on this was "what does the 'at' mean?" I see now that it's copied from another library, but "atXPath" really doesn't make me think "xpath for single element".

My second thought is that this feature is actually no longer needed. The original feature request was to avoid the need for an intermediate variable when you know you're getting back one result:

$firstresult=$xml->xpath('element[@called="XYZ"]');
$secondresult=$firstresult[0]->xpath('child[@named="ABC"]');
$finalresult=$secondresult[0];

But in all versions of PHP since 5.4 you can simply add the [0] on the end of the function call anyway:

$finalresult=$xml->xpath('element[@called="XYZ"]')[0]->xpath('child[@named="ABC"]')[0];

Like the original proposal for an extra parameter, this extends to selecting whichever result you want, as long as you're sure it exists.

Example: https://3v4l.org/1k9rk

If you don't know for sure that there will be enough results, then chaining will give an error either way, and this isn't really any different from any other method that might or might not return the expected object.

Regards,

--
Rowan Collins
[IMSoP]


--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to