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
Class contains 1 abstract method and must therefore be declared abstract or implement the remaining methods
提问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
分解
- Your class
Validate
only contains1
abstract method. - The class that
Validate
extends which isBaseValidator
has2
abstract methods set. - That means your original class (
Validate
) requires the second abstract method to be specified within it (in this case that would besetRange()
) to be set.
- 你的类
Validate
只包含1
抽象方法。 Validate
扩展的类BaseValidator
具有2
抽象方法集。- 这意味着您的原始类 (
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 setRange
class as it is extended your BaseValidator
class.
您将需要与上述相同的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";
}
}
}