> ## 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.

# PHP Public Property

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`.

```php theme={"dark"}
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.

```php theme={"dark"}
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 (`::`).

```php theme={"dark"}
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.

```php theme={"dark"}
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.

<div
  style={{ 
display: "flex", 
justifyContent: "space-between", 
alignItems: "center", 
maxWidth: "754px", 
padding: "1rem 0",
marginBottom: "24px"
}}
>
  <span style={{ fontWeight: "bold", fontSize: "1.25rem", color: "var(--tw-prose-headings)", fontFamily: "Inter, ui-sans-serif, system-ui, sans-serif" }}>Tired of Poor PHP Skills? Fix That With Deep Grasping!</span>

  <a
    href="https://syntblaze.com"
    target="_blank"
    style={{ 
  marginLeft: "24px",
  textDecoration: "none", 
  backgroundColor: "#007AFF",
  color: "#ffffff", 
  padding: "6px 16px", 
  borderRadius: "16px",
  fontSize: "0.9rem",
  fontWeight: "600",
  textAlign: "center",
  transition: "background-color 0.2s ease"
}}
  >
    Learn More
  </a>
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/skill-tracking.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=b9b0305c93bb501c9e767b5c76c88835" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/skill-tracking.png" />

  <img src="https://mintcdn.com/syntblazellc/23tyuOzaWS88qFlc/images/nuggets.png?fit=max&auto=format&n=23tyuOzaWS88qFlc&q=85&s=c86c80197299762989e9b882419b2109" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/nuggets.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/bite-sized-exercises.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=a65f9a38c37ff28ab73ed783c53c60e3" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/bite-sized-exercises.png" />
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap", marginTop: "12px" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/mastery-chain.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=748a1763454713e679260fbb95f154a2" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/mastery-chain.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-previews.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=242f61448ff5dd6deaaab2dccc13b507" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-previews.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-explanations.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=cf0fc1c31f9cd0fc26716781be05fbc9" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-explanations.png" />
</div>
