I have an object that has a property link. I get it with getLink(). This property is actually an array so I get the 1st element $link[0] which in turn is an object so I get the property getHref(). My total code looks like this.
$link = $obj->getLink();
$link0 = $link[0];
$href = $link0->getHref();
Is there a better way to write this code, like in 1 line? something like $obj->getLink()->get(0)->getHref() I’m using PHP 5.3.4
That’s not very elegant though.
Your code should probably look like this:
If
getLinkalways returns an array it should be calledgetLinks(plural) and I would expect to have to iterate over the return value; working only with the first result seems weird. Alternatively,getLinkcould accept a parameter to return a specific link, e.g.:PHP doesn’t support direct return value dereferencing, so either the object interface is not very well suited for the language it’s written in or you’re not using the object as expected.