6

I learn PHP and I have problem:

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

Now I have error:

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

Why?

pihezitoni
  • 73
  • 1
  • 1
  • 3

2 Answers2

7

According to the OOP principles, every class, that contains at least one abstract method is considered abstract as well.From the PHP Manual:

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

So you should change

class ProtectVis

with

abstract class ProtectVis
mighTY
  • 188
  • 8
0

Declare abstract class for ProtectVis because you are using abstract method

<?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";
        }
    }
Amit Gaud
  • 756
  • 6
  • 15