2

I have found no information about this in the documentation, but it seems logical to exist. How to make a type hinting for a returning a value of object type? (I'm talking about any object, not a certain object of for instance \DateTime etc.)

function getParticipant(): ?object
{
   //...
  $x = new Cat(); 
  return $x;
}

It is not working and what is really unfair is that print getType($x) will result in object. So we have an object type but can't say strictly that we will return an object?

You may say that it is really not necessary and I could have just written : Cat, but that's not what I need when I overwrite a method which has got * @return object|null */ in his PHPDoc.

2
  • 4
    You can see the RFC here. As you can see, it was accepted for PHP 7.2 just last month Commented Jun 19, 2017 at 13:41
  • wow, thats nice to find out. Can you make an answer with your comment so that I could mark it as accepted? Commented Jun 19, 2017 at 14:30

3 Answers 3

5

Type-hinting for a generic object doesn't yet exist in PHP, but should be available from version 7.2.0 onwards as a result of RFC Object typehint which passed by 32 votes to 3 on 31st May 2017.

Although normally (where possible) it is still better to type-hint to a specific class or interface.

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

2 Comments

>Although normally (where possible) it is still better to type-hint to a specific class or interface... I'd agree if we had generics in PHP but sometimes the "object" is so needed!
Arrgh, thank you so much, I've been incredulously staring at this error message for an hour, just couldn't get over it: Fatal error: Uncaught TypeError: Argument 1 passed to ...() must be an instance of object, instance of SomeClass given... :) I was on 7.1, but my CLI is 7.2, so, it's nice to be able to confirm that, indeed, it has now been fixed. (BTW, funny to see how this got fixed via an RFC, not a mere bug report. ;-p )
1

:object does not resolve for variables which have this type. In fact, PHP will look for class named object and expect that instance of this class will be returned.

Using your example you should indicate that function will return an instance of Cat so it should look like this:

function getParticipant(): Cat
{
    return new Cat();
}

I'd say that it's fine that PHPDoc says that object will be returned. IDE's should resolve it and for PHP interpreter it doesn't matter.

In case you're overwriting method you can simply overwrite @return statement in PHPDoc.

1 Comment

The thing is IDE (phpstorm for instance) would be highlighting overwritten method in which you return a certain object if the parent method was just an object. Overwriting PHPDoc is ok, but still it tastes bad.
1

I can't find an exact explanation but at the moment you can return Classes, scalar values and since PHP 7.1 you have void as return value. But in your case "object" as return value doesn't exist.

And in my eyes it make a lot more sense to check which object you return to be clear that you return the correct object. So in your case:

function getParticipant(): Cat
{
   //...
  $x = new Cat(); 
  return $x;
}

Then you can be sure that your function return the object you expect.

1 Comment

I am aware of all this. As I stated in my original post I needed it to match the PHPDoc of the function which I was going to re-write and I use type hinting in my current project so I wanted to use it everywhere.

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.