Class contains 1 abstract method and must therefore be declared abstract or implement the remaining methods

user3656133 picture user3656133 · May 21, 2014 · Viewed 53.5k times · Source

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");
?>

Answer

Darren picture Darren · May 21, 2014

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

Breakdown

  1. Your class Validate only contains 1 abstract method.
  2. The class that Validate extends which is BaseValidator has 2 abstract methods set.
  3. That means your original class (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