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.

13 comments
Avatar
Vladimir Luchansky
Comment actions Permalink

Hello,

I cannot reproduce this in 2018.1.4 and 2018.2 EAP.

What PhpStorm version do you run?

0
Comment actions Permalink

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
Comment actions Permalink

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
Comment actions Permalink

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
Comment actions Permalink

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

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

0
Comment actions Permalink

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
Comment actions Permalink

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
Comment actions Permalink

Found a related issue: https://youtrack.jetbrains.com/issue/WI-41977. Please feel free to vote/comment

1
Comment actions Permalink

Try changing your PHP language level to 7.2 or greater.

0
Comment actions Permalink

Also have a same trouble.
PHP 7.4
PhpStorm 2021.1.2 Build #PS-211.7142.44
Windows 10 Pro 64

0
Comment actions Permalink

Please describe the problem you're experiencing in details & attach screenshots if possible.

The originally reported problem doesn't occur in 2021.1.2.

0
Comment actions Permalink

Have the same problem with 2021.3.1:

 

0
Comment actions Permalink

Try to update to 2021.3.2. Checking it in that version & I'm not seeing this problem.

0

Please sign in to leave a comment.