Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

using php Can we instantiate abstract class?

Posted on 2014-04-23
3
468 Views
Last Modified: 2014-04-24
using php Can we instantiate abstract class?

<?php

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() {
        User::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?
0
Comment
Question by:rgb192
3 Comments
 
LVL 55

Accepted Solution

by:
Julian Hansen earned 250 total points
ID: 40019417
No you cannot instantiate an abstract class. You can only instantiate a class that extends an abstract class

http://www.php.net/manual/en/language.oop5.abstract.php
0
 
LVL 109

Assisted Solution

by:Ray Paseur
Ray Paseur earned 250 total points
ID: 40019794
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.
http://www.php.net/manual/en/language.oop5.abstract.php#82111
http://www.php.net/manual/en/language.oop5.abstract.php#103118
0
 

Author Closing Comment

by:rgb192
ID: 40021864
thanks
can only instantiate a child class of abstract method
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

These days socially coordinated efforts have turned into a critical requirement for enterprises.
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

861 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question