A protected property in PHP is a class member variable defined with theDocumentation Index
Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt
Use this file to discover all available pages before exploring further.
protected access modifier, restricting its visibility strictly to the declaring class and any classes that inherit from it (subclasses). It cannot be accessed or mutated directly from outside the class hierarchy via an object instance.
Syntax
Protected properties are declared using theprotected keyword. As of PHP 7.4, they fully support type declarations.
Visibility and Access Rules
The PHP engine enforces the following scope resolution rules for protected properties:- Declaring Class: Full read/write access via
$this->propertyName. - Subclasses (Child Classes): Full read/write access via
$this->propertyName. - Same-Type Object Access: Objects within the same inheritance hierarchy can access each other’s protected properties. A method belonging to a class in the hierarchy can access
$otherInstance->propertyNamedirectly, provided$otherInstanceis an object of a class within that same hierarchy. - Global Scope / External Context: Access is denied. Attempting to read or write a protected property from an external context results in a
Fatal error: Uncaught Error: Cannot access protected property.
Mechanical Example
The following code demonstrates the inheritance mechanics, including the cross-instance access boundaries of a protected property:Technical Characteristics
Visibility Widening (Overriding) When a subclass overrides a protected property, PHP’s inheritance rules dictate that the visibility can be maintained or widened, but never restricted. A subclass can redeclare aprotected property as public, but attempting to redeclare it as private will trigger a Fatal Error.
protected modifier can be combined with the static keyword. The same visibility rules apply, but the property belongs to the class itself rather than an instance. It is accessed using the scope resolution operator (::) via self::$property, parent::$property, or static::$property (for Late Static Binding).
ReflectionProperty::getValue() or ReflectionProperty::setValue(). The previously required setAccessible(true) method call is now a no-op and is no longer necessary to interact with protected members via Reflection.
Master PHP with Deep Grasping Methodology!Learn More





