note 60179 deleted from language.oop5.abstract by googleguy
| From: | googleguy@php.net | Date: | Tue, 22 Oct 2013 14:55:10 +0000 |
| Subject: | note 60179 deleted from language.oop5.abstract by googleguy | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-197234@lists.php.net to get a copy of this message | ||
Note Submitter: rasto_klc (at) yahoo (dot) obvious
----
Variable-length argument lists in abstract methods will generate fatal error if derived. Here is an
simple example:
<?php
// common wrap for all validators is forcing uniform interface
abstract class ValidatorWrap {
// just example why variable-length arguments are needed
public function __construct()
{
if (func_num_args() > 0) {
$arg_list = func_get_args();
call_user_func_array(array(&$this, 'setupValidator'), $arg_list);
} else {
$this->setupValidator();
}
// continue with construction
}
// amount of arguments is specific to validator implementation
abstract public function setupValidator();
// known interface
abstract public function validate($value);
}
class Validator1 extends ValidatorWrap {
protected $pattern = '';
// this will generate PHP Fatal error because $pattern is not expected
public function setupValidator($pattern)
{
$this->pattern = $pattern;
}
// this will do OK
public function validate($value)
{
return preg_match($this->pattern, $value);
}
}
// make numeric validator
$validator = new Validator1('/^\d+$/');
echo (int) $validator->validate($_REQUEST['digits']);
?>
I need it to work so I just redefine troublemaking function as follows:
<?php
public function setupValidator() { }
?>
This will give me functionality I need and generates only PHP Strict Standards warning.