Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Node.js, PHP and APIs

Devsense PHP Tools shows an undefined property warning in VS Code

ErrorA squiggle under a property name with the message that the property is unknown or undefined

Short answer

The extension reports a property it cannot find on the class. First decide if it is a real gap: declare the property, or add an @property tag when __get or __set provides it. If the code is intentional, hide only that diagnostic code with .editorconfig, php.problems.exclude, or an @suppress tag on the class, not all problems.

  • Applies to VS Code, PHP Tools (Devsense) extension, PHP 8.3, PHP 8.4
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Open the Problems panel and read the code (the docs list the unknown-property warning as PHP0416, Unknown field use). Use the code your panel shows in every setting below.

    View > Problems (Ctrl+Shift+M)
  2. 2Match what you see, then jump to its fix

    1. 1The warning points at $this->something or $obj->something that no class body declaresThe property is not declared on the classFix
    2. 2The class has magic __get or __set, and the extension reports a hint about a magic propertyThe property comes from __get or __set but is not annotatedFix
    3. 3The code is correct for your design, and you only want this warning silentThe pattern is intentional and you want one diagnostic hiddenFix
    4. 4Only one legacy class needs the exceptionYou want it silent for one class or function onlyFix

What does the error look like?

  • A squiggle under a property name with the message that the property is unknown or undefined
  • The same warning listed in the VS Code Problems panel with a PHP code

Cause 1 of 4

The property is not declared on the class

1Check

The diagnostic means the property is unknown to the analyzer. On PHP 8.2 and later, creating such a property at runtime is also deprecated, unless the class opts in or is stdClass.

grep -n "public \$\|private \$\|protected \$" src/Order.php

2Fix

Declare the property. The PHP manual lists declaring it as the preferred fix for dynamic properties. #[AllowDynamicProperties] on the class is the opt-in for code you cannot change.

class Order
{
    public ?string $note = null;
}

3Verify

The squiggle disappears after the extension re-analyses the file.

php -l src/Order.php

Confirmed by php.net

Did this fix it?

Cause 2 of 4

The property comes from __get or __set but is not annotated

1Check

The extension's diagnostics list says a property handled by magic __get or __set may be unintentional when it is not declared with an @property annotation while other properties are.

grep -n "__get\|__set\|@property" src/Model.php

2Fix

Describe the magic properties in the class docblock.

/**
 * @property string $title
 * @property int $views
 */
class Model
{
    public function __get($name) { /* ... */ }
}

3Verify

The hint no longer appears for those names.

php -l src/Model.php

Confirmed by docs.devsense.com

Did this fix it?

Cause 3 of 4

The pattern is intentional and you want one diagnostic hidden

1Check

The extension can hide problems per diagnostic code. A rule with an error code beats a general php_diagnostic rule.

cat .editorconfig 2>/dev/null

2Fix

Add a code rule to .editorconfig (false, off or none hides it), or exclude the code in VS Code's settings.json. Use the code from your Problems panel.

# .editorconfig
[*.php]
php_diagnostic_php0416 = false

// settings.json (an array hides only the listed codes in that path)
"php.problems.exclude": {
    "src/legacy/": [416]
}

3Verify

Reload the window and confirm the warning is gone for those files only.

Developer: Reload Window (Command Palette)

Confirmed by docs.devsense.com

Did this fix it?

Cause 4 of 4

You want it silent for one class or function only

1Check

A docblock tag suppresses problems for the function or class it is attached to.

grep -n "@suppress" -r src | head

2Fix

Add @suppress with the code to that docblock. Several codes can be separated by a comma or a space.

/**
 * @suppress PHP0416
 */
class LegacyReport
{
}

3Verify

The warning is gone inside that class and still shows elsewhere.

Developer: Reload Window (Command Palette)

Confirmed by docs.devsense.com

Did this fix it?

How do I stop it coming back?

  • Declare properties on the class; it also keeps your code clean on PHP 8.2 and later.
  • Hide single codes in a small path, not everything with true.
  • Commit .editorconfig so the team sees the same Problems list.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Back end

Apache returns 403 when a URL contains %3F

a request like /page%3Fname=test is refused while /page?name=test works

Recent Apache httpd blocks a rewrite when the request contains an encoded question mark (%3F) and the rewritten target has a literal ?.

2 causes, each with a fix

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.