Take the State of PHP 2026 Survey →

Object typehint View as Markdown

Implemented (PHP 7.2)
Authors
Michał Brzuchalski michal.brzuchalski@gmail.com, Dan Ackroyd danack@php.net
Date
2016-08-12
Version
1.1
Source
wiki.php.net/rfc/object-typehint

Votes

Accepting the object typehint RFC for PHP 7.2? closed
Yes
32
No
3
35 voters
ajf Yes 2017-05-19 01:54
ashnazg No 2017-05-17 14:05
cmb Yes 2017-05-18 11:14
colinodell Yes 2017-05-17 16:35
danack Yes 2017-05-17 12:14
daverandom Yes 2017-05-31 17:05
davey Yes 2017-05-17 14:05
dm Yes 2017-05-19 21:34
dragoonis Yes 2017-05-18 17:06
emir Yes 2017-05-17 12:43
galvao Yes 2017-05-17 21:14
jedibc Yes 2017-05-24 11:38
jhdxr No 2017-05-17 17:09
kalle Yes 2017-05-17 23:51
kelunik Yes 2017-05-18 13:57
kguest Yes 2017-05-30 13:57
krakjoe Yes 2017-05-17 13:19
lcobucci Yes 2017-05-18 14:41
leigh Yes 2017-05-17 13:15
leszek Yes 2017-05-24 07:10
levim Yes 2017-05-17 14:47
mariano Yes 2017-05-23 04:50
narf Yes 2017-05-17 13:50
nikic Yes 2017-05-17 16:32
ocramius Yes 2017-05-17 15:36
peehaa Yes 2017-05-17 13:50
pmjones Yes 2017-05-17 14:34
pollita Yes 2017-06-01 03:15
salathe Yes 2017-05-22 11:47
sammyk Yes 2017-05-17 13:10
sebastian Yes 2017-05-17 15:30
stas No 2017-05-17 16:24
trowski Yes 2017-05-26 03:30
weierophinney Yes 2017-05-18 21:33
zeev Yes 2017-06-01 11:11
Object type should implement variance? closed
Yes
10
No
20
30 voters
ashnazg No 2017-05-17 14:05
bwoebi Yes 2017-05-17 12:15
danack No 2017-05-17 16:01
daverandom Yes 2017-05-31 17:05
davey Yes 2017-05-17 14:08
dm Yes 2017-05-19 21:32
dmitry No 2017-05-25 10:11
dragoonis No 2017-05-18 17:06
emir No 2017-05-22 13:06
galvao Yes 2017-05-17 21:17
jedibc Yes 2017-05-24 11:40
jhdxr No 2017-05-17 17:09
jmikola Yes 2017-05-17 18:01
kalle No 2017-05-17 23:51
kguest No 2017-05-30 14:04
krakjoe Yes 2017-05-17 13:19
lcobucci No 2017-05-18 14:42
leigh Yes 2017-05-17 13:15
levim No 2017-05-17 14:09
mariano No 2017-05-23 04:51
narf No 2017-05-17 14:54
nikic No 2017-05-17 16:32
ocramius No 2017-05-18 13:53
pmjones No 2017-05-17 15:36
pollita Yes 2017-06-01 03:15
sammyk No 2017-05-17 14:54
sebastian No 2017-05-17 15:32
stas No 2017-05-17 16:25
trowski No 2017-05-26 03:30
zeev No 2017-06-01 11:12

Discussions on the mailing list

[RFC][DISCUSSION] Object type hint
· 20 messages
[RFC][VOTE] Object typehint
· 18 messages
[RFC][DISCUSSION] Object type hint, now with added variance
by Dan Ackroyd · 21 messages
Change in type-hint representation
by Dmitry Stogov · 1 message

RFC text

Copied from wiki.php.net, last synchronized Oct 13, 2026 11:51 UTC

Introduction

PHP 7 introduced scalar types for parameters and also for declaring return types for functions.

However it is not currently possible to declare that a function either needs to be passed an object as a parameter, or to declare that a function must return an object.

Proposal

This RFC proposes that object should be used as a parameter type and as a return type. Any object would pass the type check.

Passing a value that is not an object to a parameter that is declared as type object would fail the type check, and a TypeError would be thrown.

Similarly, if a function is declared as returning an object but does not return an object, again a TypeError would be thrown.

As it would be used internally, object would become a reserved classname, unavailable for use as a class name in userland code.

For class methods that use object as either a parameter or return type, the inheritance checks would use contravariance for parameter types, and covariance for return types.

Examples

Parameter type

function acceptsObject(object $obj) {
    ...
}
 
// This code can be statically analyzed to be correct
acceptsObject(json_decode('{}'));
 
// This code can be statically analyzed to be correct
acceptsObject(new \MyObject());
 
// This can be statically analysed to contain an error.
// and would throw an TypeError at runtime.
acceptsObject("Ceci n'est pas une object.");

Return type

Functions and methods can be declared with an object return type, to enforce that the function must return an object.

// This function can be statically analysed to conform to the
// return type
function correctFunction() : object {
    $obj = json_decode('{}');
 
    return $obj;
}
 
// This function can be statically analysed to contain an error
// and will also fail at runtime.
function errorFunction() : object {
    return [];
}

Benefits

Make code easier to understand

Although most code deals with specific types, there are several types of library where it is common to handle arbitrary object types.

Hydrators + data extractor

Hydrators allow objects to be filled with data from an array. Extractors do the opposite, they take an object and return an array of data.

interface Hydration {
  // Hydrate an object by populating data
  public function hydrate(array $data, object $object) : object;
}
 
interface Extraction {
  // Extract values from an object
  public function extract(object $object) : array;
}

The extraction step can take an arbitrary object as the sole parameter. The hydration step can take an arbitrary object as the second parameter, and will return an arbitrary object. Having the type for the parameters and the return type be set as object will make the expected types be clearer to anyone using these functions, as well as detect incorrect types if there is an error in the code.

Service containers and DIC libraries

For both service containers and dependency injection libraries, it is common to want to put services and other objects into the container.

interface ServiceContainer {
  // Set service definition
  public function set(string $id, object $service);
 
  // Retrieve service object from container
  public function get(string $id) : object;
}

Additionally, ORM libraries such as Doctrine have functions that will either consume or produce arbitrary objects.

Catching return errors

Having an object return type would allow some errors to be detected more quickly. The following function is meant to return an object. With the object return typ set for the function, failing to return an object would cause a TypeError to be thrown in the location where the bug is.

function unserialize($data) : object {
 
    $type = $data['type'];
 
    switch ($type) {
        case 'foo': { return new Foo(); }
        case 'bar': { return new Bar(); }
        case 'zot': { new zot(); }  // Ooops, this is an error
    }
}

Without the object return type, an incorrect value of null is returned from the function. This error can only be found by debugging at runtime.

Enforcing signature in inheritance

Currently, as PHP does not allow 'object' to be used as a return type, it is not possible to enforce the return type in a child class. In this example the method is supposed to return an object, but a programmer has changed what the function does in the child class.

class WidgetFactory {
    function create() {
        return new Widget();
    }
}
 
class CustomWidgetFactory extends WidgetFactory {
    function create() {
        $object = new Widget();
 
        return true; //This is an error that cannot be statically analyzed.
    }
}

This type of error can only be detected when running the code.

If we had object as a type, even if a programmer misunderstood what the method was supposed to do, and accidentally tried to create a child class that had a different signature, the code would not compile, and the error would be caught before the code is run:

class WidgetFactory {
    function create() : object {
        return new Widget();
    }
}
 
class CustomWidgetFactory extends WidgetFactory {
    // This class would not compile, as the signature of the metod in
    // the child class is not compatible with the method signature in 
    // the parent class.
    function create() : bool {
       ...
    }
}

If the programmer wrote the correct signature for the method in the child class, but returned the wrong value, this error would also be caught:

class WidgetFactory {
    function create() : object {
        return new Widget();
    }
}
 
class CustomWidgetFactory extends WidgetFactory {
    function create() : object {
        $object = new Widget();
 
        // returning something that is not an object throws a TypeError exception.
        return true;
    }
}

This would also be an error detectable by a static analyzer.

Variance

Argument typehint contravariance

Classes that extend another class, or implement an interface may broaden a parameter type from a specific class, to more generic 'object' typehint.

class Foo {
}
 
class Bar {
    public function foo(Foo $object) : object {
        return $object;
    }
}
 
class Baz extends Bar {
    public function foo(object $object) : object {
        return $object;    
    }
}

This is normally known as contravariant.

Classes extending/implementing may not narrow method arguments types from 'object' to more specific type.

class Foo {
}
 
class Bar {
    public function foo(object $object) : object {
        return $object;
    }
}
 
class Baz extends Bar {
    public function foo(Foo $object) : object {
        return $object;    
    }
}

In this situation the standard PHP warning for incompatible signatures will be raised.

Declaration of Baz::foo(Foo $object): object should be compatible with Bar::foo(object $object): object

Return type covariance

Classes extending/implementing may narrow methods return types from 'object' to specified class name. This behavior is the similar as that proposed for 'iterable' and is normally called covariant.

class Foo {
}
 
class Bar {
    public function foo(object $object) : object {
        return $object;
    }
}
 
class Baz extends Bar {
    public function foo(object $object) : Foo {
        return $object;    
    }
}

In above example 'object' return type was narrowed to 'Foo' type in extending class.

Reflection

There are two changes in reflection:

  • ReflectionType::isBuiltin returns true for parameters and return types declared as 'object'.

  • ReflectionType::__toString returns 'object' for parameters and return types declared as 'object'.

Backward Incompatible Changes

Although 'object' is already a soft reserved word, this RFC adds object as fully reserved classname.

Proposed PHP Version(s)

PHP 7.2.

RFC Impact

To SAPIs

None.

To Existing Extensions

None.

Unaffected PHP Functionality

This doesn't affect the behaviour of cast operators.

Proposed Voting Choices

Voting will run from 2017-05-17, for two weeks, until 2017-05-31. As this is a language change, a 2/3 majority is required. The vote is a straight Yes/No vote for accepting the RFC and merging the patch. The additional vote is also a straight Yes/No vote for accepting variance behaviour on the object type.

Vote: Accepting the object typehint RFC for PHP 7.2? — Yes: 32 · No: 3 (closed)

Vote: Object type should implement variance? — Yes: 10 · No: 20 (closed)

Patches and Tests

  • PR#2080
  • Online Runtime Execution at 3v4l.org on tab “RFC Branches”

Discussions