Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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");
?>
like image 286
user3656133 Avatar asked May 21 '14 06:05

user3656133


People also ask

Which class containing abstract method must be declared abstract?

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.

Is every class containing abstract method must be declared abstract?

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.

When a method in a class is declared abstract the class must be declared?

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.

How many abstract methods should an abstract class have 1 point?

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.


1 Answers

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

like image 77
Darren Avatar answered Oct 24 '22 06:10

Darren