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

An object in PHP is a compound data type that encapsulates state (properties) and behavior (methods) into a single, distinct entity. It serves as an instantiated runtime representation of a class blueprint, occupying a specific memory space and referenced via an internal object identifier.

## Instantiation

Objects are created using the `new` keyword followed by the class name. This allocates memory for the object and invokes its constructor method (if defined) to initialize its state.

```php theme={"dark"}
class Entity {
    public string $type;
    
    public function __construct(string $type) {
        $this->type = $type;
    }

    public function process(): string {
        return "Processing: " . $this->type;
    }
}

$instance = new Entity('Node');
```

## Object Access Operators

PHP utilizes specific operators to interact with an object's internal members:

* **Object Operator (`->`):** Used to access non-static properties and methods.
* **Nullsafe Operator (`?->`):** Introduced in PHP 8.0, it short-circuits the evaluation, returning `null` if the object on the left side is `null`, preventing fatal errors.

```php theme={"dark"}
// Standard access
$instance->type = 'Service';
$instance->process();

// Nullsafe access
$nullableInstance = null;
$result = $nullableInstance?->process(); // Evaluates to null
```

## Memory and Assignment Mechanics

Unlike scalar data types (integers, strings), PHP objects are not strictly passed or assigned by value. An object variable contains an **object identifier** rather than the object data itself. When an object is assigned to another variable or passed to a function, PHP copies the identifier, meaning both variables point to the same underlying instance.

```php theme={"dark"}
$objA = new stdClass();
$objA->value = 10;

$objB = $objA; // Copies the object identifier
$objB->value = 99;

echo $objA->value; // Outputs: 99
```

To create a distinct, independent copy of an object, the `clone` keyword must be used, which performs a shallow copy and triggers the `__clone()` magic method if defined.

```php theme={"dark"}
$objC = clone $objA;
$objC->value = 42;

echo $objA->value; // Outputs: 99 (remains unchanged)
```

## `stdClass` and Type Casting

PHP provides a built-in, generic empty class called `stdClass`. It is the default class used when casting other data types (like arrays or scalars) into objects.

When an associative array is cast to an `object`, the array keys become object properties.

```php theme={"dark"}
$dataArray = [
    'id' => 104,
    'status' => 'active'
];

$dataObject = (object) $dataArray;

echo $dataObject->status; // Outputs: active
```

## Anonymous Objects

PHP supports the instantiation of objects from anonymous classes. These are objects created without a named class declaration, allowing for the immediate instantiation of an object that can optionally extend a class or implement interfaces.

```php theme={"dark"}
interface LoggerInterface {
    public function log(string $message): void;
}

$logger = new class implements LoggerInterface {
    public function log(string $message): void {
        echo $message;
    }
};

$logger->log("System initialized.");
```

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