Solved

using php Can we instantiate interface?

Posted on 2014-04-23
6
1,358 Views
Last Modified: 2014-04-28
using php Can we instantiate interface?

please give example if possible.
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
  • 2
6 Comments
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 40018945
http://www.php.net/manual/en/language.oop5.abstract.php

Classes defined as abstract may not be instantiated...
0
 

Author Comment

by:rgb192
ID: 40018982
but it could possibly be done for abstract classes in java according to this link:
http://stackoverflow.com/questions/13670991/interview-can-we-instantiate-abstract-class


is there proof it can not be done for interfaces in php

(you sent abstract php.net link)
0
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 250 total points
ID: 40018989
Methinks you edited your question after I posted.

Interfaces do not have logic of their own. They simply define contracts that ensure a class offers some subset of functionality. Using interfaces, you can group different types of objects (i.e. classes) into categories based on the functionality they expose, rather than the type they inherit from. For example, say you had an interface called IGrow. This interface defines the contract that all classes which implement it have a method named "Grow". What kinds of things grow? Children grow. Plants grow. Loan interest grows. My love for PHP grows.... You see where I'm going, here? Different things can all "grow", but they don't have to extend the same base class. Yet because each class would implement the IGrow interface, you know that each class has a Grow method.

So back to your question:  Can you instantiate an interface? The answer is no. But you can instantiate a concrete class that implements that interface. And you can use a variable that is of the interface type to work with the objects generically rather than working with each concrete type. That is, I can have an array of the aforementioned objects:

$growers = array(
    "child" => new Child,
    "flower" => new Flower,
    "interest" => new Interest,
    "php_love" => new PhpLove
);

Open in new window


...and I can create some generic code that makes each grow:

for ($growers as $grower) {
    $grower->Grow();
}

Open in new window


More info: http://www.php.net/manual/en/language.oop5.interfaces.php
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 110

Assisted Solution

by:Ray Paseur
Ray Paseur earned 250 total points
ID: 40019802
Java !== PHP

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

Executive summary: An abstract class is a partially built class, requiring you to provide some, but maybe not all, of the methods.  An interface is a contract between developers.  You must provide all of the methods to comply with the interface contract.
0
 

Author Comment

by:rgb192
ID: 40021863
kaufmed
could you please provide example of grow interface
0
 

Author Closing Comment

by:rgb192
ID: 40028772
thanks for examples I will ask a followup question soon.
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

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…
This article discusses how to create an extensible mechanism for linked drop downs.
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.

696 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