?
Solved

using php Can we instantiate interface?

Posted on 2014-04-23
6
Medium Priority
?
1,541 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 1000 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
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.

 
LVL 111

Assisted Solution

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

Technology Partners: 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!

Question has a verified solution.

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

Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
This article discusses how to implement server side field validation and display customized error messages to the client.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Suggested Courses

762 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