Solved

using php Can we instantiate abstract class?

Posted on 2014-04-23
3
448 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 51

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 108

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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Suggested Solutions

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
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 count occurrences of each item in an array.

707 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now