Documentation Index
Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt
Use this file to discover all available pages before exploring further.
A private property in PHP is a class member variable declared with the private visibility modifier. It enforces strict encapsulation by restricting read and write access exclusively to the lexical scope of the class in which it is explicitly defined. It cannot be accessed from the global scope, nor can it be accessed by inheriting (child) classes.
Syntax and Declaration
Private properties are declared using the private keyword, optionally followed by a type declaration (PHP 7.4+). They can be defined as instance properties or static properties.
class Configuration {
private $legacyProperty;
private string $typedProperty;
private int $initializedProperty = 100;
private readonly array $immutableProperty; // PHP 8.1+
private static int $counter = 0; // Static property
}
Constructor Property Promotion (PHP 8.0+)
Private instance properties can be declared and initialized directly within the constructor signature, eliminating boilerplate assignment code.
class Database {
public function __construct(
private string $dsn,
private string $username
) {}
}
Access Rules and Scope
1. Internal Access
Within the defining class, instance private properties are accessed using the object operator (->) on the $this pseudo-variable. Static private properties are accessed using the scope resolution operator (::) with the self keyword.
class Token {
private string $hash;
private static int $generationCount = 0;
public function __construct(string $hash) {
$this->hash = $hash; // Instance access
self::$generationCount++; // Static access
}
}
2. External Access and Magic Methods
Attempting to read or write a private property from outside the class scope does not immediately result in a fatal error if the __get() or __set() magic methods are defined. PHP will invoke these methods to handle the inaccessible property. A fatal Error is only thrown if these magic methods are absent.
class Token {
private string $hash = 'abc-123';
public function __get(string $name) {
if ($name === 'hash') {
return 'intercepted';
}
}
}
$token = new Token();
echo $token->hash; // Outputs: "intercepted" (No fatal error)
3. Inheritance and Shadowing
Private properties are not inherited by child classes. If a child class attempts to access a parent’s private property, PHP treats it as an undefined property in the child’s scope.
If a child class declares a property with the exact same name as a parent’s private property, it creates a completely distinct, shadowed property. The parent’s methods will continue to interact with the parent’s private property, while the child’s methods will interact with the child’s property.
class ParentClass {
private string $data = 'Parent Data';
public function getParentData(): string {
return $this->data;
}
}
class ChildClass extends ParentClass {
private string $data = 'Child Data'; // Shadows the parent property
public function getChildData(): string {
return $this->data;
}
}
$obj = new ChildClass();
echo $obj->getParentData(); // Outputs: "Parent Data"
echo $obj->getChildData(); // Outputs: "Child Data"
Class-Level Visibility Resolution
In PHP, visibility is resolved at the class level, not the instance level. This means an object can access the private properties of another object, provided both objects are instances of the exact same class.
class Node {
public function __construct(private int $id) {}
public function isSameNode(Node $otherNode): bool {
// Valid: Accessing a private property of a different instance of the same class
return $this->id === $otherNode->id;
}
}
Dynamic Access Mechanisms
While standard syntax blocks external access, PHP provides internal mechanisms that can dynamically read or modify private properties:
Reflection API:
$node = new Node(42);
$reflection = new ReflectionProperty(Node::class, 'id');
// In PHP 8.1.0+, setAccessible(true) is no longer required, but historically used
$reflection->setAccessible(true);
echo $reflection->getValue($node); // Outputs: 42
Closure Binding:
$node = new Node(99);
$extractor = function() {
return $this->id;
};
$boundExtractor = $extractor->bindTo($node, Node::class);
echo $boundExtractor(); // Outputs: 99
Master PHP with Deep Grasping Methodology!Learn More