tags:

views:

63

answers:

3
+2  Q: 

PHP return value

If a static method returns an object .. can I in one line get one of the objects attributes? Something like this but in a single line :

$obj = Parser::GetFirstItem();
$strTitle = $obj->Title;
+3  A: 

Sure. Just try it out:

$strTitle = Parser::GetFirstItem()->Title;

Not entirely sure when this was introduced. 5? 5.1? 5.2? Will have to check.

Update: Seems to have been a PHP 5 feature from the start.

Pekka
that'll be $strTitle = ... not $obj. ;)
Spudley
@Spudley of course, cheers.
Pekka
+3  A: 

Have you actually tried it?

$strTitle = Parser::GetFirstItem()->Title;

That should work, provided you're using PHP5 and not still stuck on PHP4.

Here's a link to an article about it.

Spudley
A: 

Your GetFirstItem method should returns an object. Only virtual (not static methods) can do this:

return $this;
Alexander.Plutov
`$this` won't work in a static context
Pekka
yes. read my changes.
Alexander.Plutov
Using `return self;` in a static function returns an `E_NOTICE` that `self` is an undefined constant, assumes you meant `'self'`, and returns a string. (Note: PHP 5.3.2). If you want an object of the class that has the static function, then you should use `return new self();`
Aether