• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 533
  • Last Modified:

using php Can we instantiate abstract class?

using php Can we instantiate abstract class?


abstract class DomainObject {
  public $inDomainObject=1;

class User extends DomainObject {
  public $inUser=2;
    public static function create() {
        return new User();        

class Document extends DomainObject {
  public $inDocument=3;
    public static function create() {
        //return new Document();
        //return new User(); 
        return new DomainObject();

$document = Document::create();
print_r( $document );

Open in new window

Fatal error: Cannot instantiate abstract class DomainObject in C:\wamp\www\POPP-edition4-code\9781430260318_Chapter_04_Code\listing4.09.php on line 20

why does this example not work?
2 Solutions
Julian HansenCommented:
No you cannot instantiate an abstract class. You can only instantiate a class that extends an abstract class

Ray PaseurCommented:
This fails because line 3 defines DomainObject as abstract.  But line 20 uses the DomainObject class with the new keyword.

Abstract classes and Interfaces share some characteristics.  Neither can be instantiated.  These notes are explanatory and to my way of thinking pretty accurate.
rgb192Author Commented:
can only instantiate a child class of abstract method
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Train for your Pen Testing Engineer Certification

Enroll today in this bundle of courses to gain experience in the logistics of pen testing, Linux fundamentals, vulnerability assessments, detecting live systems, and more! This series, valued at $3,000, is free for Premium members, Team Accounts, and Qualified Experts.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now