Undefined class object

Answered
/**
* @param MyClass|MySecondClass|object $myArgument
*
* @return array
*/
private function myMethod(object $myArgument): array
{

 

The `object` in the DocBlock is highlighted and the message says: "Undefined class object".

 

This is not correct as the `object` type is allowed by PHP.

8 comments

Hello,

I cannot reproduce this in 2018.1.4 and 2018.2 EAP.

What PhpStorm version do you run?

0

I have the same warning shown for my source code, using 2018.1.5 - all plugins etc. are up to date. In my example I am using the following docblock:

/**
* @param object $class
*/

Even adding "object" as type hint in the function definition as is allowed in PHP 7.2 does not change the warning about the docblock (although PHPStorm does not complain about the object typehint - I have set the PHP version to 7.2)

0

My version is this:

PhpStorm 2018.1
Build #PS-181.4203.565, built on March 28, 2018
JRE: 1.8.0_152-release-1136-b20 x86_64
JVM: OpenJDK 64-Bit Server VM by JetBrains s.r.o
macOS 10.13.4

And those are the screenshots:



0

Just updated the IDE:

PhpStorm 2018.1.5
Build #PS-181.5281.19, built on June 7, 2018
JRE: 1.8.0_152-release-1136-b39 x86_64
JVM: OpenJDK 64-Bit Server VM by JetBrains s.r.o
macOS 10.13.4

Problem still remains...

0

Could you please check if renaming "object" to something else helps?

There was a similar issue with classes named after reserved words.

0

Everything else works - if I use "string" instead of "object" it works fine in terms of displaying a warning. When I write "obje" it even offers to autocomplete to object - but then complains that object is undefined. The name of the variable does not seem to matter either.

I also tried it in different contexts and classes - the warning is always shown.

0

But I want to use exactly the reserved word: this method accepts two kind of classes: `EntityA` and `EntityB`.

But I cannot type hint for both, so I use the generic `object`.

 

Then in the method I check for the real type and throw an exception if the passed `object` is not a type of one of the two allowed

/**
* @param ClassA|ClassB|object $classAOrB
*
* @return MyResultObject
*/
public function myMethod(object $classAorB): MyResultObject
{
if ( ! $classAorB instanceof ClassA && ! $classAorB instanceof ClassB) {
throw new \InvalidArgumentException('TheHandlingClass::myMethod() accepts only a ClassA or a ClassB as first argument.');
}

So, I don't need to rename anything as I'm using a reserved word and want to use it.

The problem is that PHPStorm doesn't understand that the reserved word that I'm using is a reserved one and that it really exists.

 

0

Please sign in to leave a comment.