rfc:incompat_ctx

This is an old revision of the document!


Request for Comments: How to write RFCs

An RFC for deprecating and removing `$this` from incompatible context.

Introduction

This RFC proposes deprecating (in PHP 5.5) and removing (in the next version) the “`$this` from incompatible context feature.

The feature

To be clear, the feature I'm proposing to remove is this:

class A { function foo() { var_dump(get_class($this)); } } class B { function bar() { A::foo(); } } $b = new B; $b->bar(); //string(1) "B"

Internal methods cannot be called in this fashion.

Rationale

Method implementations almost always assume that $this refers to an instance of a compatible type. When they are called with an incompatible `$this`, there'll a bug most of the time. The only warning is an E_STRICT message, which many people have disabled.

Because this feature is surprising and little-known, a simple error like calling Class::foo() instead of ::fooStatic() (when foo() and fooStatic() have similar names), by not failing on the call site, can be difficult to identify.

When combined with LSB, it can be even more difficult to identify. See https://bugs.php.net/bug.php?id=62446

What this proposal is not about

This proposal is not about removing static calls to instance methods or instance-like calls to static methods.

BC break

The break should be minor. I very much doubt there are many lines of code that rely of it. It has been discouraged with an E_STRICT for a while.

This feature, as noted in the [comment](http://lxr.php.net/xref/PHP_TRUNK/Zend/zend_vm_def.h?r=80497ea7dfade2ccd032ef65103c0a113338653a#2335), exists only for compatibility with PHP 4. Far more used features legacy features, like register globals, have already been axed.

This feature can, however, be used to implement trait-like behavior, and I'm sure someone somewhere did such a thing.

Changelog

  • 2012-07-30: Initial version
rfc/incompat_ctx.1343668838.txt.gz · Last modified: 2017/09/22 13:28 (external edit)