Type hinting for properties in PHP 7?

Type hinting for properties in PHP 7?

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

Stefan Bogdanescu

Founder & Senior Architect

Type Hinting for Properties in PHP 7: A Reality Check

If you are migrating legacy codebases or starting a new project, you might be looking for a way to enforce strict types on class properties using syntax similar to method arguments. The desire is logical: ensure that $bar can only hold an instance of Bar at compile time, triggering a hard error if you attempt to assign NotBar.

In PHP 7, the answer is nuanced but definitive: you cannot use type hints directly on class properties. While PHP 7 introduced return type declarations and argument type hints, it intentionally omitted property type declarations. The syntax you proposed—public $bar : Bar;—is invalid in PHP 7 and will result in a ParseError.

This limitation stems from how PHP handles variable scoping and memory management at that time. Property types were not part of the language specification until PHP 8.0, where they were introduced alongside union types and nullable types. If you are running PHP 7 today, your code must rely on other mechanisms to achieve similar type safety guarantees.

The Role of DocBlocks in PHP 7

Since native syntax is unavailable, the standard approach for adding type information in PHP 7 is using DocBlocks with @var annotations. This provides static analysis tools, such as PHPStan or Psalm, with the necessary context to detect type violations during development without relying on runtime enforcement.

Here is how you structure that:

class Foo {
    /**
     * @var Bar
     */
    public $bar;
}

While this looks clean and self-documenting, it is crucial to understand that DocBlocks are ignored by the PHP interpreter at runtime. They are purely for static analysis. If you run the following code with standard PHP 7:

class Foo {
    /**
     * @var Bar
     */
    public $bar;
}

$fooInstance = new Foo();
// Assuming Bar is defined in the namespace
$fooInstance->bar = new NotBar(); // This runs without error

The assignment succeeds. PHP treats $fooInstance->bar as a dynamic property capable of holding any object unless you implement additional logic to prevent it. The static analyzer will flag this as an issue if you install the correct configuration, but the application itself will not crash immediately upon assignment.

Achieving Runtime Enforcement with Accessors

If strict runtime enforcement is required in PHP 7, you must move the type checking logic into your own code. The most common pattern involves using accessor methods (getters and setters) that explicitly validate the incoming data. This shifts the responsibility from the language syntax to application logic.

class Foo {
    /**
     * @var Bar|null
     */
    public $bar;

    public function setBar($value): void {
        if ($value instanceof Bar) {
            $this->bar = $value;
        } else {
            throw new InvalidArgumentException('Value must be an instance of Bar');
        }
    }
}

By using a setter, you gain the ability to throw exceptions when invalid types are assigned. This effectively mimics the behavior of type hints, ensuring that $fooInstance->bar never holds a NotBar object unless you explicitly bypass the setter and access the property directly (which is generally considered a code smell in strict architectures).

The Path Forward: PHP 8.0+

If your project allows for an upgrade to PHP 8.0 or later, you can utilize native property type declarations. This removes the boilerplate of setters and provides compile-time safety where the language itself prevents invalid assignments.

class Foo {
    public Bar $bar; // Native property type hint
}

With this syntax, attempting to assign new NotBar() to $fooInstance->bar results in a fatal error immediately upon assignment (or even during object construction if the property is initialized). This brings PHP 7's limitations into line with modern static typing standards.

Conclusion on Strategy

When working within the constraints of PHP 7, the choice between DocBlocks and custom setters depends on your workflow. If you use a robust Static Analysis tool like PHPStan configured with @phpstan-ignore or similar directives, DocBlocks provide excellent developer experience without runtime overhead. However, if you require hard guarantees that invalid types cannot exist in your objects at runtime, implementing setter methods is the necessary trade-off until you can migrate to PHP 8.

Our network