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.

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.
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.
// 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.
$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.
$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.
$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.
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.");
Master PHP with Deep Grasping Methodology!Learn More