php 类包含 1 个抽象方法,因此必须声明为抽象方法或实现其余方法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23775250/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 16:56:13  来源:igfitidea点击:

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

phpfatal-error

提问by user3656133

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 包含 1 个抽象方法,因此必须在第 21 行的 C:\wamp\www\jump\task3\day8\abstract.php 中声明为抽象方法或实现其余方法 (BaseValidator::SetRange)

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

回答by Darren

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

致命错误:Class Validate 包含 1 个抽象方法,因此必须在第 21 行的 C:\wamp\www\jump\task3\day8\abstract.php 中声明为抽象方法或实现其余方法 (BaseValidator::SetRange)

Breakdown

分解

  1. Your class Validateonly contains 1abstract method.
  2. The class that Validateextends which is BaseValidatorhas 2abstract 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.
  1. 你的类Validate只包含1抽象方法。
  2. Validate扩展的类BaseValidator具有2抽象方法集。
  3. 这意味着您的原始类 ( Validate) 需要在其中指定第二个抽象方法(在本例中为setRange())。

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 setRangeclass as it is extended your BaseValidatorclass.

您将需要与上述相同的setRange课程,因为它是您的BaseValidator课程的扩展。

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

例子