ZetCode

PHP object Keyword

last modified November 25, 2026

The PHP object type, introduced in PHP 7.2, accepts any object instance. It is a type declaration for parameters, return values, and properties, and it excludes every non-object value.

This article shows where the type can be used, which values satisfy it, why instanceof object never matches, and when a class or interface is the better choice.

Basic definitions

The object type, available since PHP 7.2, accepts any object instance. It can be used for parameters, return values, and properties, and it is the widest type that still excludes scalars, arrays, resources, and null.

object is a type name, not a class. It cannot be instantiated with new, it has no methods or properties, and it says nothing about what the object can do. A class or interface type is more precise whenever the required behaviour is known; object is the fallback for code that genuinely works with arbitrary objects.

Unlike scalar parameters, an object parameter performs no coercion. Passing a string, an integer, an array, or null raises TypeError even without strict_types. For a nullable object, declare ?object.

An object parameter

The first example accepts any object and reports its class.

obj_param.php
<?php

declare(strict_types=1);

function describe(object $value): string
{
    return "object of class " . $value::class;
}

$user = new stdClass();
$user->name = "Ada";

echo describe($user), "\n";
echo describe(new DateTimeImmutable("2026-01-01")), "\n";

The function is called with a stdClass instance and with a DateTimeImmutable; both satisfy the object declaration. $value::class returns the class name of the actual instance, which is the only information the type guarantees.

$ php obj_param.php
object of class stdClass
object of class DateTimeImmutable

An object return type

The same type can describe what a function returns.

obj_return.php
<?php

declare(strict_types=1);

function makeUser(string $name): object
{
    $user = new stdClass();
    $user->name = $name;

    return $user;
}

$user = makeUser("Ada");
echo $user->name, "\n";

The declaration promises an object, and the caller may rely on that much. It does not promise a name property, so an IDE or static analyser cannot check the property access. Returning a dedicated class, or an array with a documented shape, gives the caller a stronger contract.

$ php obj_return.php
Ada

An object property

A property may be typed as object as well. The property must then hold an object before it is read, and any later assignment is checked against the type.

obj_property.php
<?php

declare(strict_types=1);

class Logger
{
    private object $writer;

    public function setWriter(object $writer): void
    {
        $this->writer = $writer;
    }

    public function log(string $message): string
    {
        return $this->writer->write($message);
    }
}

$logger = new Logger();
$logger->setWriter(new class {
    public function write(string $message): string
    {
        return "log: $message";
    }
});

echo $logger->log("ready"), "\n";

try {
    $logger->setWriter("file");
} catch (TypeError $e) {
    echo get_class($e), "\n";
}

The logger stores a writer and calls its write method. The property type stops a string from being assigned, and the attempt raises TypeError, which is exactly the mistake that would fail later with a less obvious error. The type still does not prove that the object has write; an interface such as Writer would express that requirement, and the PHP Interfaces tutorial shows how.

$ php obj_property.php
log: ready
TypeError

What counts as an object

Instances of user classes are objects, and so are closures, dates, array objects, and enum cases. The instanceof operator, however, does not test the object type.

obj_kinds.php
<?php

declare(strict_types=1);

enum Suit: string
{
    case Hearts = "H";
}

$values = [
    new stdClass(),
    new DateTimeImmutable("2026-01-01"),
    new ArrayObject([1, 2]),
    fn() => 1,
    Suit::Hearts,
];

foreach ($values as $value) {
    echo get_debug_type($value), " ",
        var_export(is_object($value), true), " ",
        var_export($value instanceof object, true), "\n";
}

Every value is an object according to is_object, yet $value instanceof object is false for all of them. instanceof expects a class or interface name on the right, and no class called object exists. Use is_object() for a runtime check of the type; a check of this kind is rarely needed when the declaration already says object.

$ php obj_kinds.php
stdClass true false
DateTimeImmutable true false
ArrayObject true false
Closure true false
Suit true false

object versus a class type

A class type accepts only that class and its subclasses, while object accepts everything.

obj_class.php
<?php

declare(strict_types=1);

class User
{
    public function __construct(public string $name)
    {
    }
}

function acceptObject(object $value): string
{
    return get_debug_type($value);
}

function acceptUser(User $user): string
{
    return $user->name;
}

echo acceptObject(new stdClass()), "\n";
echo acceptUser(new User("Ada")), "\n";

try {
    acceptUser(new stdClass());
} catch (TypeError $e) {
    echo get_class($e), "\n";
}

Both functions accept the User instance, but only acceptObject accepts a stdClass; the second call fails with TypeError. Widening a parameter to object makes a function easier to call and harder to use, because the body must then discover what it received. Start with the narrowest type that covers the real callers.

$ php obj_class.php
stdClass
Ada
TypeError

Union types and null

object may be combined with other types in a union, and ?object adds null.

obj_union.php
<?php

declare(strict_types=1);

function render(string|object $value): string
{
    return is_object($value) ? get_debug_type($value) : $value;
}

echo render("plain text"), "\n";
echo render(new DateTimeImmutable("2026-01-01")), "\n";

function maybe(?object $value): string
{
    return $value === null ? "nothing" : get_debug_type($value);
}

echo maybe(null), "\n";
echo maybe(new stdClass()), "\n";

The union string|object is valid and is narrowed inside the function with is_object. A union that contains object and a class name, such as object|User, is a compile-time error because the class is already covered by object. The same rule rejects object|Stringable and object|mixed.

$ php obj_union.php
plain text
DateTimeImmutable
nothing
stdClass

Best practices

Choose the declaration according to what the code needs from the value.

DeclarationAccepts
objectany object, including closures and enum cases
UserUser and its subclasses
Stringableobjects with a __toString method
mixedevery value, including scalars and null

Prefer a class or interface when the required methods are known. Use object at boundaries that truly accept anything, then narrow with instanceof before calling a method. Declare ?object when the value may be absent, and avoid catching the TypeError from a wrong argument in normal flow; fix the call instead.

Source

PHP manual: objects

PHP manual: type declarations

PHP manual: is_object

This article covered the PHP object type: parameters, return values, properties, the values it accepts, its difference from a class type, and its use in unions. Continue with PHP class keyword, PHP Interfaces, and PHP mixed Type for the neighbouring type declarations.

Author

I am Jan Bodnar, a programmer and technical writer. I have been writing programming tutorials at ZetCode since 2007 and have published eight e-books. See about the author.

List all PHP tutorials.