> ## 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 Anonymous Class

An anonymous class in PHP is an unnamed, single-use object blueprint instantiated at the exact moment of its declaration. Introduced in PHP 7.0, it provides a mechanism to create disposable objects that encapsulate state and behavior without polluting the global namespace with formal class definitions.

## Basic Syntax

The declaration and instantiation occur simultaneously using the `new class` construct.

```php theme={"dark"}
$anonymousObj = new class {
    public string $property = 'value';

    public function method(): string {
        return $this->property;
    }
};

echo $anonymousObj->method(); // Outputs: value
```

## Constructor Arguments

Arguments can be passed to the anonymous class constructor by placing parentheses immediately after the `class` keyword.

```php theme={"dark"}
$multiplier = 2;

$mathObj = new class($multiplier) {
    public function __construct(private int $factor) {}

    public function calculate(int $number): int {
        return $number * $this->factor;
    }
};

echo $mathObj->calculate(5); // Outputs: 10
```

## Inheritance, Interfaces, and Traits

Anonymous classes support the full PHP object-oriented paradigm. They can extend base classes, implement multiple interfaces, and consume traits exactly like named classes.

```php theme={"dark"}
$complexObj = new class('Data') extends AbstractModel implements JsonSerializable, Stringable {
    use LoggerTrait;

    public function __construct(private string $data) {}

    public function jsonSerialize(): mixed {
        return ['data' => $this->data];
    }

    public function __toString(): string {
        return $this->data;
    }
};
```

## Internal Engine Naming

While conceptually "anonymous," the PHP engine assigns a unique internal identifier to the class at runtime. This identifier is based on the memory address or the file path and line number where the class was declared.

```php theme={"dark"}
$obj = new class {};
echo get_class($obj); 
// Outputs an internal name like: class@anonymous/path/to/script.php:10$0
```

Because the engine caches the class definition based on its declaration point, multiple instantiations from the exact same declaration line (e.g., inside a loop) will share the same internal class type, meaning `$obj1 instanceof $obj2` will evaluate to `true`.

## Scope and Context

An anonymous class does **not** inherit the variable scope of the context in which it is defined. When declared inside a method of an outer class, the anonymous class cannot automatically access the outer class's `private` or `protected` members.

To grant access to the outer scope, you must explicitly pass the required data via the constructor, or the anonymous class must explicitly extend the outer class.

```php theme={"dark"}
class OuterClass {
    private string $secret = 'hidden';

    public function getAnonymous() {
        // The anonymous class has no implicit access to $this->secret
        // It must be injected via the constructor.
        return new class($this->secret) {
            public function __construct(private string $injectedSecret) {}

            public function reveal(): string {
                return $this->injectedSecret;
            }
        };
    }
}
```

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