Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

using php Can we instantiate interface?

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

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

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

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
This article discusses how to implement server side field validation and display customized error messages to the client.
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…
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.
Suggested Courses

636 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