Solved

using php Can we instantiate interface?

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

please give example if possible.
0
Comment
Question by:rgb192
  • 3
  • 2
6 Comments
 
LVL 74

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 74

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
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
LVL 108

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
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…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

706 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now