Ferenjito Ferenjito - 2 years ago 114
PHP Question

Visibility in PHP - public fields of parent classes

class A {
public $myField = "A";

class B extends A {
function __construct() {
$this->myField = "B"; // My IDE complains 'Field declared dynamically'

I use PHP 7.0.18 and recently upgraded from PhpStorm 2016 to PhpStorm 2017.2 as my IDE.

Since I upgraded, the IDE complains, that fields that are declared in parent classes were not accessible and therefore declared dynamically -
which does not make any difference in runtime but is still annoying and leads me to the question whether this is a bug in PhpStorm 2017 or I misunderstood the visibility of fields in PHP so far.

Answer Source

It's an issue in PhpStorm 2017.2 -- hopefully (should be) fixed for 2017.2.1 final.

https://youtrack.jetbrains.com/issue/WI-36285 -- watch this ticket (star/vote/comment) to get notified on any progress.

Right now you may just disable that particular inspection if that bothers you too much:

  • place caret on the error/warning,
  • hit Alt + Enter,
  • find right entry (usually top most one),
  • use Arrow Right to show sub menu and choose "Disable Inspection".

Alternatively just find it in a usual Settings/Preferences | Editor | Inspections screen.

P.S. Providing correct PHP code without missing keywords and other basic errors straight away helps a lot as well. With your original code (and even the one that is till there in your question) it's not possible to easily tell if you are new to PHP and simply do not know exactly what you are doing .. or if it's indeed an issue with IDE.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download