2017-05-28 73 views
-1

我學習PHP和我有問題:致命錯誤:類XXX包含1種抽象方法,因此必須聲明爲抽象或實現其餘的方法

<?php 
class ProtectVis 
{ 
    abstract protected function countMoney(); 
    protected $wage; 

    protected function setHourly($hourly) 
    { 
     $money = $hourly; 
     return $money; 
    } 
} 

class ConcreteProtect extends ProtectVis 
{ 
    function __construct() 
    { 
     $this->countMoney(); 
    } 
    protected function countMoney() 
    { 
     echo "ok"; 
    } 
} 
$worker = new ConcreteProtect(); 

現在我有錯誤:

Fatal error: Class ProtectVis contains 1 abstract method and must therefore be declared abstract or implement the remaining methods (ProtectVis::countMoney) in

爲什麼?

+0

你應該在'ConcreteProtect'類中定義一個名爲'countMoney'的函數。 –

+0

定義了這個函數。 – pihezitoni

+0

您必須將ProtectVis類聲明爲'abstract',因爲它包含'abstract'方法。 –

回答

0

根據面向對象的原則,每一個類,包含至少一個抽象方法被認爲是抽象爲well.From PHP手冊:

Classes defined as abstract may not be instantiated, and any class that contains at least one abstract method must also be abstract.

所以,你應該改變

class ProtectVis 

abstract class ProtectVis 
0

申報ProtectVis抽象類,因爲你正在使用抽象方法

<?php 
    abstract class ProtectVis 
    { 
     abstract protected function countMoney(); 
     protected $wage; 

     protected function setHourly($hourly) 
     { 
      $money = $hourly; 
      return $money; 
     } 
    } 

    class ConcreteProtect extends ProtectVis 
    { 
     function __construct() 
     { 
      $this->countMoney(); 
     } 
     protected function countMoney() 
     { 
      echo "ok"; 
     } 
    } 
相關問題