Evening Rowan, I must protest: It is wasteful, unclear, and generally nasty, to create, return, and manipulate an array if you are only ever going to reference one element in code.
It is quite normal to know the structure of a document ahead of time, and this would allow you to write more succinct and less wasteful code. It would appear valuable to me ... I'm not in love with the name of the thing, but the basic idea appears valuable. Cheers Joe On Sat, Jan 7, 2017 at 4:26 PM, Rowan Collins <rowan.coll...@gmail.com> wrote: > 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 > >