Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fatal error when extending abstract class in PHP

I'm learning advanced level of OOP PHP..(Or I want to learn :))

This is my code.

Excerpt:

<?php 
abstract class Karakter
{
    abstract public function isim($name);
    abstract public function yas($age);

    public function yazdir()
    {
        print $this->isim() . " " . $this->yas();
    }
}

class Insan extends Karakter
{
    public $isim;
    public $yas;
    public function isim()
    {
        return "Bu adamın ismi: " . $this->isim;
    }

    public function yas()
    {
        return "Bu adamın yaşı: " . $this->yas;
    }
}

When I run this code I can't win through. I can see this error:

Fatal error: Declaration of Insan::isim() must be compatible with that of Karakter::isim() in C:\AppServ\www\OOP\1.php on line 26
like image 827
Yusuf Ali Bozkır Avatar asked Aug 31 '26 21:08

Yusuf Ali Bozkır


1 Answers

You have defined the function isim in the abstract class with one parameter.

abstract public function isim($name);

In order to correctly implement this function in any subclass you must override the function with exactly one parameter:

class Insan extends Karakter {
    public function isim($name) {
       [..]
    }

    ...
}
like image 89
halfdan Avatar answered Sep 02 '26 10:09

halfdan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!