A public property in PHP is a class member variable declared 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.
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 thepublic 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.
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$thispseudo-variable. - External Access: Accessed outside the class using the object operator (
->) on the object instance.
Technical Characteristics
- Legacy Declaration: If a property is declared using the legacy
varkeyword (originating from PHP 4) instead of an explicit access modifier, it acts as a direct alias forpublic. In modern PHP,varremains 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
protectedorprivate). 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
statickeyword, the property belongs to the class itself rather than an instance. It is accessed globally using the scope resolution operator (::).
- Mutability and Readonly: By default, public properties are fully mutable. As of PHP 8.1, the
publicmodifier can be combined with thereadonlymodifier. This allows unrestricted read access from any scope, but restricts write access strictly to initialization within the class scope.
- 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_DEPRECATEDnotice (and will throw anErrorstarting 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 likestdClass. 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





