Re: protected abstract static function ... E_STRICT
| From: | Christoph Becker | Date: | Thu, 12 Mar 2015 00:02:42 +0000 |
| Subject: | Re: protected abstract static function ... E_STRICT | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-324625@lists.php.net to get a copy of this message | ||
Johannes Ott wrote:
> why PHP in Strict mode fires as warning for the declaration of a
> abstract function:
Good question! Having abstract static functions without late static
binding doesn't make sense, but that "feature" has been introduced with
PHP 5.3.0. See also an respective RFC draft[1], which proposes to
remove the E_STRICT for this case.
> What I want to do is:
>
>
> abstract class Scalar {
>
> protected $value;
>
> public function __construct($value) {
> if (static::valid($value)) {
> $this->value = $value;
> } else {
> throw new RuntimeException();
> }
> }
>
> /** each child has to have it's own function */
> protected abstract static function valid($value);
> }
>
> final class Integer extends Scalar {
>
> protected static function valid($value) {
> return is_int($value);
> }
> }
>
> final class Float extends Scalar {
>
> protected static function valid($value) {
> return is_float($value) || is_int($value);
> }
>
> }
>
> The abstract superclass has no need implement the valid function but the
> childclasses have to.
>
> What I'm having as workaround at the moment:
>
> abstract class Scalar {
>
> ...
>
> protected static function valid($value) {
> throw new BadMethodCallException('Method should be only called
> in subclass');
> }
>
> }
>
> Any better solutions for this?
I would suggest to rename the wording, to make it clear that a subclass
has to implement this method -- it's not so much about the caller.
An alternative might be to disable E_STRICT while the function is being
compiled.
However, I would probably prefer to make the constructor abstract, or to
use a dynamic method isValid().
[1] <https://wiki.php.net/rfc/reclassify_e_strict#abstract_static_methods>
--
Christoph M. Becker