Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP: Fatal error: Cannot instantiate abstract class

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?

like image 745
klor Avatar asked May 27 '17 16:05

klor


People also ask

Can you instantiate an abstract class PHP?

PHP has abstract classes and methods. Classes defined as abstract cannot be instantiated, and any class that contains at least one abstract method must also be abstract.

Why can you not instantiate an abstract class?

We cannot instantiate an abstract class in Java because it is abstract, it is not complete, hence it cannot be used.

Can I instantiate an abstract class in C++?

An abstract class is, conceptually, a class that cannot be instantiated and is usually implemented as a class that has one or more pure virtual (abstract) functions. A pure virtual function is one which must be overridden by any concrete (i.e., non-abstract) derived class.

What is an abstract class in Java?

Abstract class: is a restricted class that cannot be used to create objects (to access it, it must be inherited from another class). Abstract method: can only be used in an abstract class, and it does not have a body. The body is provided by the subclass (inherited from).


2 Answers

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;
like image 52
arbogastes Avatar answered Oct 05 '22 23:10

arbogastes


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.

like image 42
T. AKROUT Avatar answered Oct 05 '22 21:10

T. AKROUT