Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Property hook cannot be both final and private

Description

When a property is final, it cannot be overridden in a child class; when a class is private, it is only available in the current class.

Hence, a property cannot be final and private at the same time.

In this situation, the property may be final, due to the property hooks. A property without hooks cannot be final.

Example

<?php

class X {
	private $p {
		final get => 2;
		set => $this->p = $value;
	}
}

?>

Alternatives

  • Remove the hooks, and use the magic methods __get/__set.
  • Remove the final keyword.
  • Remove the private keyword and use protected or public.