OO Parameter Checking
| From: | Christian Schneider | Date: | Fri, 29 Feb 2008 17:27:12 +0000 |
| Subject: | OO Parameter Checking | ||
| Groups: | php.internals | ||
| Request: | Send a blank email to internals+get-35863@lists.php.net to get a copy of this message | ||
Marcus Boerger wrote:
> you still cannot ignore basic inheritance or reuse rules. Protocols
> have to be respected -> E_FATAL, fix your code.
I have several comments (plus patches) to this:
1) The current checks are IMHO too strict regarding default values for
parameters: An inheriting class can add default values to a parameter
without breaking the protocol:
class A { function foo($x) { ... } }
class B extends A { function foo($x = 42) { ... } }
should be allowed. Only if there are fewer parameters in B::foo or if
B::foo has more *required* parameters than A::foo the protocol is
broken. Otherwise every instance of B can be passed to something
expecting A.
The attached paramcheck_relaxed.patch.txt for HEAD changes this.
2) The current checks are IMHO too strict regarding static functions:
Static functions are not part of an instance's protocol (they can not be
passed around) and should be left out of the check the same way
constructors are ignored. The use case for this is a factory method:
class A { static function factory($size) { ... } }
class B extends A { static function factory($size, $color) { ... } }
The attached paramcheck_relaxed.patch.txt for HEAD changes this.
3) I guess more controversial is the change from E_STRICT to
E_COMPILE_ERROR for PHP 6 regarding these checks. While you consider
such OO code as broken I would highly appreciate it if you recognize
that other people have other coding standards and/or old code to
maintain. Adhering to the
http://en.wikipedia.org/wiki/Liskov_substitution_principle
is something
strict OO coders want to do. And they are using E_STRICT anyway, so I
see no reason to force everybody else to change their code, so it should
be left a simple E_STRICT IMHO.
The attached paramcheck_e_strict.patch for HEAD changes this back to the
old behaviour.
If paramcheck_relaxed.patch.txt gets accepted I'd volunteer to back-port
it to 5.x as well.
Regards,
- Chris