Solved

using php Can we instantiate abstract class?

Posted on 2014-04-23
3
486 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 57

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 110

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

Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

Question has a verified solution.

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

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.  …
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
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…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

724 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