PHP: Fatal error: Cannot instantiate abstract class

34,646

Solution 1

Classes defined as abstract may not be instantiated, and any class that contains at least one abstract method must also be abstract. You can read about this in PHP's documentation here: link

Here's an example.

There is an abstract class (note that abstract methods don't have body - they CAN'T have body - it's just a signature):

abstract class AbstractClass
{
    // Force Extending class to define this method
    abstract protected function getValue();
    abstract protected function prefixValue($prefix);

    // Common method. It will be available for all children - they don't have to declare it again.
    public function printOut() {
        print $this->getValue() . "\n";
    }
}

Extend your abstract class with a class like this (note that all abstract methods MUST be defined in concrete class):

class ConcreteClass1 extends AbstractClass
{
    protected function getValue() {
        return "ConcreteClass1";
    }

    public function prefixValue($prefix) {
        return "{$prefix}ConcreteClass1";
    }
}

Then you can create instance of ConcreteClass1:

$class1 = new ConcreteClass1;

Solution 2

Your class should not be abstract:

class my_database extends database {
  public function set_value($value) {
    $this->value = $value;
  }
}

In OOP, abstract class can't be instanciated juste it can be extended.

Share:
34,646
klor
Author by

klor

Web developer, Unix system admin, Moodle admin, Moodle developer. Knowledge: Windows, Linux, Debian, Apache, MySQL, PHP, HTML, CSS, Moodle, etc.

Updated on May 29, 2020

Comments

  • klor
    klor almost 4 years

    I have an abstract database class named as:

    abstract class database {
      protected $value;
    }
    

    I created another abstract class

    abstract class my_database extends database {
      public function set_value($value) {
        $this->value = $value;
      }
    }
    

    When I try to use it:

    $my_db = new my_database();
    

    I get error:

    Fatal error: Cannot instantiate abstract class my_database in ...
    

    What I try to do is: The abstract class database has a protected $value and I would like to create a wrapper class, to be able to change the protected value (temporarily).

    How can I do that?

    EDIT1: unfortunately earlier, when I tried without abstract my_database, I got the errors:

    - abstract methods and must therefore be declared abstract or implemented
    - Abstract function cannot contain body
    

    EDIT2: After taking out the abstract word completely from my_database, I got the following error:

    Fatal error: Class my_database contains 32 abstract methods and must therefore be declared abstract or implement the remaining methods

    How can I fix this?

    • rickdenhaan
      rickdenhaan almost 7 years
      You cannot call new (abstract class). In this use case, my_database should not be abstract.
    • Vanya Avchyan
      Vanya Avchyan almost 7 years
      Classes defined as abstract may not be instantiated php.net/manual/en/language.oop5.abstract.php
    • Nana Partykar
      Nana Partykar almost 7 years
      Abstract classes cannot be instantiated, but they can be sub classed.
    • β.εηοιτ.βε
      β.εηοιτ.βε almost 7 years
      When you tried without abstract on my_database you did inadvertently added abstract to your public function set_value, hence your error message. Retry.
  • klor
    klor almost 7 years
    Edited my post.
  • arbogastes
    arbogastes almost 7 years
    @klor Read my answer again - "any class that contains at least one abstract method must also be abstract" - if your class has an abstract method then make your class abstract and extend it by concrete class as is shown in my anwer.
  • klor
    klor almost 7 years
    After your solution I got the following error: Fatal error: Class my_database contains 32 abstract methods and must therefore be declared abstract or implement the remaining methods
  • arbogastes
    arbogastes almost 7 years
    @klor So your class my_database is extending some abstract class that have 32 abstract methods. In your my_database you must implement every one of this methods.
  • klor
    klor almost 7 years
    Can I replicate the methods of the original abstract without the need to copy/paste the remaining abstract methods of it?
  • arbogastes
    arbogastes almost 7 years
    @klor When inheriting from an abstract class, all methods marked abstract in the parent's class declaration must be defined by the child.
  • klor
    klor almost 7 years
    Just a reference to the abstract method or the complete method code?
  • arbogastes
    arbogastes almost 7 years
    @klor I edit my answer, so this may help you. In short - abstract method don't have a body - this way you force children of an abstract class to define body of this abstract method.
  • klor
    klor almost 7 years
    Thanks very much for your instructions!