PHP Access to Undeclared Static Property

PHP Access to Undeclared Static Property

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

PHP Access to Undeclared Static Property

When you see a Fatal Error: Uncaught Error: Cannot access undeclared static property while trying to assign values to class members inside a constructor, it feels like a contradiction in terms. You are explicitly defining those properties right above the line that throws the error. The confusion arises from a subtle but critical distinction between instance properties and static properties, and how the self keyword interacts with them.

In your provided code examples for the monster and item classes, you have declared fields like $id, $name, and $baseLevel as private instance properties. However, inside the constructor, you are attempting to access them using self::$id. This syntax tells PHP to look for a property named id on the class itself (a static property), not on the specific object instance being created.

Since your class definition does not include any static properties declared with the public static, protected static, or even private static keyword, PHP treats $id as an instance variable only. When the interpreter encounters self::$id, it checks the class scope for a static member named id. Finding none, it raises the fatal error you are seeing.

To resolve this, you need to switch from accessing properties via self:: to accessing them via $this->. The $this object reference points to the current instance of the class, which is exactly where your private variables live. Your constructor logic should look like this:

class monster{
    private $id = 0;
    private $name = "";
    // ... other properties
    
    function __construct($a, $b, $c, $d, $e, $f, $g, $h){
        $this->id = $a;
        $this->name = $b;
        $this->baseLevel = $c;
        $this->attack = $d;
        $this->defense = $e;
        $this->baseEXP = $f;
        $this->dropType = $g;
        $this->dropNum = $h;
    }
}

The same correction applies to your item class. Every line using self::$propertyName must be updated to $this->propertyName. This is not just a syntax fix; it aligns the code with standard object-oriented principles where constructors initialize state on the specific object instance, not on the class blueprint.

There is a valid scenario where you would intentionally use self:: inside a method: when dealing with static properties or constants defined within the class. For example, if you had public static $config = [], then self::$config would be correct. However, mixing static access syntax for instance variables creates ambiguity and leads to runtime failures like this one.

If you were working in a modern framework ecosystem, such as Laravel, you might encounter similar confusion when dealing with service providers or Eloquent models where static facades are common. In those contexts, accessing the underlying instance often requires explicit instantiation or using helper methods rather than direct static property access on unrelated classes. For more details on how Laravel handles model initialization and attribute setting, you can refer to https://laravelcompany.com for documentation on their specific implementation patterns. However, in standard PHP development without a framework, adhering strictly to $this-> for instance variables ensures your code remains predictable and free from scope resolution errors.

Ultimately, the error message is a direct hint: "undeclared static property." The fix is declaring the property as an instance property (which you already did) and accessing it via the instance pointer ($this) rather than the class pointer (self).

Our network