Fatal error: Class Validate contains 1 abstract method and must therefore be declared abstract or implement the remaining methods (BaseValidator::SetRange) in C:\wamp\www\jump\task3\day8\abstract.php on line 21
<?php
abstract class BaseValidator
{
abstract function Validate($string);
abstract function SetRange($string);
}
class Validate extends BaseValidator
{
public function Validate($string)
{
if (!preg_match('/[^A-Za-z]/', $string))
{
echo "'{$string}' contains only alphabets!";
}
if (is_numeric($string))
{
echo "'{$string}' Conatins No. Only!<br/>";
echo '<br>';
}
}
}
class setRange extends BaseValidator
{
public function SetRange($string)
{
if(!(strlen($string)>4 && strlen($string)<10))
{
echo "You are not in range of 4-10";
}
}
}
$obj = new Validate();
$obj = $obj->Validate("Hello");
$obj = new SetRange("hello");
$obj = $obj->SetRange("hello");
?>
A class which contains 0 or more abstract methods is known as abstract class. If it contains at least one abstract method, it must be declared abstract. And yes, you can declare abstract class without defining an abstract method in it.
Explanation: Every class containing abstract method must be declared abstract. Abstract class cannot be directly initiated with 'new' operator and it cannot be initiated. Statement (2) and (3) are correct.
A class which contains the abstract keyword in its declaration is known as abstract class. But, if a class has at least one abstract method, then the class must be declared abstract. If a class is declared abstract, it cannot be instantiated.
Rules to Declare Abstract Class Abstract classes cannot be instantiated directly. An abstract class must have at least one abstract method. An abstract class includes final methods.
Dumbing down the error message for you:
Fatal error: Class Validate contains 1 abstract method and must therefore be declared abstract or implement the remaining methods (BaseValidator::SetRange) in C:\wamp\www\jump\task3\day8\abstract.php on line 21
Validate
only contains 1
abstract method.Validate
extends which is BaseValidator
has 2
abstract methods set.Validate
) requires the second abstract method to be specified within it (in this case that would be setRange()
) to be set.That means you could simply set the function in your class but have it empty:
class Validate extends BaseValidator
{
public function Validate($string)
{
if (!preg_match('/[^A-Za-z]/', $string))
{
echo "'{$string}' contains only alphabets!";
}
if (is_numeric($string))
{
echo "'{$string}' Conatins No. Only!<br/>";
echo '<br>';
}
}
public function setRange($string) {}
}
SIDE NOTE:
You will require the same as above for your setRange
class as it is extended your BaseValidator
class.
class setRange extends BaseValidator
{
public function Validate($string){}
public function SetRange($string)
{
if(!(strlen($string)>4 && strlen($string)<10))
{
echo "You are not in range of 4-10";
}
}
}
Example
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With