?
Solved

using php Can we instantiate abstract class?

Posted on 2014-04-23
3
Medium Priority
?
502 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
[X]
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
3 Comments
 
LVL 58

Accepted Solution

by:
Julian Hansen earned 1000 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 111

Assisted Solution

by:Ray Paseur
Ray Paseur earned 1000 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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to dynamically set the form action using jQuery.
Suggested Courses

752 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