[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

using php Can we instantiate abstract class?

Posted on 2014-04-23
3
Medium Priority
?
509 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 59

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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
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 and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
Suggested Courses

650 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