Skip to main content

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 public property in PHP is a class member variable declared with the public access modifier, granting unrestricted read and write access to the property from any execution scope. This visibility allows the property to be accessed and modified directly from within the defining class, from inheriting subclasses, and from external code interacting with an instantiated object.

Syntax and Declaration

Public properties are declared using the public keyword, followed by an optional type declaration, and the variable name. Untyped properties implicitly default to a value of null and do not have an uninitialized state. Conversely, typed properties without an explicit default value remain in an uninitialized state until assigned, and accessing them prematurely will throw an Error.
class Entity {
    // Untyped public property (implicitly defaults to null)
    public $identifier;

    // Typed public property (uninitialized until assigned)
    public string $name;

    // Typed public property with a default value
    public int $status = 1;
}

Access Mechanics

The syntax used to access a public property depends entirely on the execution context (internal versus external object scope):
  • Internal Access: Accessed within the class methods using the object operator (->) on the $this pseudo-variable.
  • External Access: Accessed outside the class using the object operator (->) on the object instance.
class Container {
    public string $payload = 'Default';

    public function modifyPayload(): void {
        // Internal access
        $this->payload = 'Internal Modification';
    }
}

$instance = new Container();

// External read access
echo $instance->payload; 

// External write access
$instance->payload = 'External Modification'; 

Technical Characteristics

  • Legacy Declaration: If a property is declared using the legacy var keyword (originating from PHP 4) instead of an explicit access modifier, it acts as a direct alias for public. In modern PHP, var remains fully valid syntax and does not emit any deprecation notices.
  • Inheritance Rules: When a subclass extends a parent class, it inherits all public properties. The subclass can override the property’s default value, but it cannot restrict the visibility (e.g., redefining it as protected or private). Additionally, property types in PHP are strictly invariant. A child class cannot change the type of an inherited property, add a type declaration to an untyped property, or remove a type declaration from a typed property.
  • Static Public Properties: If combined with the static keyword, the property belongs to the class itself rather than an instance. It is accessed globally using the scope resolution operator (::).
class Configuration {
    public static bool $debugMode = false;
}

// Global access without instantiation
Configuration::$debugMode = true;
  • Mutability and Readonly: By default, public properties are fully mutable. As of PHP 8.1, the public modifier can be combined with the readonly modifier. This allows unrestricted read access from any scope, but restricts write access strictly to initialization within the class scope.
class ImmutableEntity {
    public readonly string $uuid;

    public function __construct(string $uuid) {
        $this->uuid = $uuid; // Legal initialization
    }
}
  • Dynamic Properties: Historically, assigning a value to an undeclared property on an object dynamically created a public property at runtime. As of PHP 8.2, dynamic property creation emits an E_DEPRECATED notice (and will throw an Error starting in PHP 9.0). To prevent this notice when a dynamic property is created, the class must be explicitly marked with the #[AllowDynamicProperties] attribute or extend specific internal classes like stdClass. Implementing the __set() magic method intercepts assignments to undeclared properties, but if the __set() method itself dynamically creates a property, the deprecation notice is still emitted.
Master PHP with Deep Grasping Methodology!Learn More