I have a problem with the code and don't know what to do. I used this
$validate = new OOP_Validate;
$validate->addValidator(new OOP_Validate_Int());
if($validate->isValid('test')) echo 'TRUE';
and show me error:
Catchable fatal error: Argument 1 passed to OOP_Validate::addValidator() must implement interface OOP_Validate_Interface, instance of OOP_Validate_Int given, called in C:\xampp\htdocs\GameV\index.php on line 21 and defined in C:\xampp\htdocs\GameV\library\OOP\Validate.php on line 37
AddValidator:
public function addValidator(OOP_Validate_Interface $validator, $breakChain = false){ $this->_validators[] = array( 'Name' => $validator, 'Break' => $breakChain, ); return $this; }
Interface:
interface OOP_Validate_Interface { public function isValid($value); public function getMessage(); }
What could be wrong?
As the error message explains, the addValidator
method requires that the first argument passed to it implements the interface called OOP_Validate_Interface
.
$validate->addValidator(new OOP_Validate_Int() /* <-- this argument must implement OOP_Validate_Interface */);
Check your code and change the class declaration for OOP_Validate_Int
to be:
class OOP_Validate_Int implements OOP_Validate_Interface
{
/*
Then ensure that all the methods required by OOP_Validate_Interface
are defined in this class.
*/
}
For more info, check the Object Interface section in the PHP manual.
The problem here is that method addValidator accepts the first parameter of type OOP_Validator_Interface but You pass it an instance of OPP_Validate_Int() here: $validate->addValidator(new OOP_Validate_Int());
.
I suggest not to explicitly retype the first argument of addValidator() (if it is not part of some framework) or in other hand a class OOP_Validate_Int must implement the OOP_Validator_Interface: class OOP_Validate_Int implements OOP_Validator_Interface { .. }
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.