rfc:instanceof_improvements

This is an old revision of the document!


PHP RFC: instanceof improvements

Introduction

As currently implemented, the `instanceof` operator is inconsistent.

var_dump(new MyClass instanceof MyClass); // true

So far so good? What if we tried to use a scalar type? They can be specified as parameter types just like class names, right?

$x = 123;
var_dump($x instanceof int); // false, right hand side is always treated as a class name

Proposal

Make instanceof support non-object types:

var_dump('foo' instanceof string);      // true
var_dump('foo' instanceof ('string'));  // true
$type = 'string';
var_dump('foo' instanceof $type);       // true

Types to support

This proposal covers only concrete scalar types int, float, string, bool and null; as well as compound types array, object, callable and iterable. Other types are intentionally omitted:

  • mixed is pointless because there are easier ways to produce an expression always evaluating to true than $something instanceof mixed.
  • void is kinda obvious, but I'm mentioning it just to be thorough.

All attempts to check against these types would evaluate to false (just as currently) and produce warnings in 8.1, upgraded to fatals in 8.2.

  • resource is not available as a parameter type and is on its way out, so it will be unaffected by this RFC and treated as a class name, available for userspace to use.

Legacy type aliases

Constant expressions on the left hand side

The current implementation has a shortcut where if there is a constant expression to the left of instanceof, the result is hardcoded as false, since the operator supports only class names on the right and constant expressions can't produce objects. This raises a question: how should be Captain Obvious cases like 123 instanceof int be treated? What about slightly less obvious cases like 123 instanceof $typeName? For comparison, I've checked two languages that have approaches to OOP similar to PHP:

  • In Java, a variable is required on the left side of instanceof. All types of expressions are disallowed.
  • In C#, constant expressions to the left of is produce a correct result but a compiler warning is issued.
  • None of these support type names as strings, so the latter use case has no direct analogs.

Backward Incompatible Changes

What breaks, and what is the justification for it?

Proposed PHP Version(s)

PHP 8.1.

RFC Impact

To SAPIs

Describe the impact to CLI, Development web server, embedded PHP etc.

To Existing Extensions

Will existing extensions be affected?

To Opcache

It is necessary to develop RFC's with opcache in mind, since opcache is a core extension distributed with PHP.

Please explain how you have verified your RFC's compatibility with opcache.

New Constants

Describe any new constants so they can be accurately and comprehensively explained in the PHP documentation.

Open Issues

Make sure there are no open issues when the vote starts!

Unaffected PHP Functionality

List existing areas/features of PHP that will not be changed by the RFC.

This helps avoid any ambiguity, shows that you have thought deeply about the RFC's impact, and helps reduces mail list noise.

Future Scope

This section details areas where the feature might be improved in future, but that are not currently proposed in this RFC.

Proposed Voting Choices

What should be done about this (improve instanceof / add new operator / reject this RFC)?

Patches and Tests

Links to any external patches and tests go here.

If there is no patch, make it clear who will create a patch, or whether a volunteer to help with implementation is needed.

Make it clear if the patch is intended to be the final patch, or is just a prototype.

For changes affecting the core language, you should also provide a patch for the language specification.

Implementation

After the project is implemented, this section should contain

  1. the version(s) it was merged into
  2. a link to the git commit(s)
  3. a link to the PHP manual entry for the feature
  4. a link to the language specification section (if any)

References

Links to external references, discussions or RFCs

Rejected Features

Keep this updated with features that were discussed on the mail lists.

rfc/instanceof_improvements.1615225258.txt.gz · Last modified: 2021/03/08 17:40 by maxsem