2

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 Answers 3

3

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.

Sign up to request clarification or add additional context in comments.

Comments

3

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.

1 Comment

that'll be $strTitle = ... not $obj. ;)
0

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

return $this;

1 Comment

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();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.