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

Cannot use the final modifier on a property

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 not final, as there are no property hooks.

Example

<?php

class X {
	final private $property;
}

?>

Alternatives

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

In more recent PHP versions, this error message is now :ref:property-cannot-be-both-final-and-private.