Re: protected abstract static function ... E_STRICT

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php





[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux