Solved

using php Can we instantiate interface?

Posted on 2014-04-23
6
1,219 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 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to dynamically set the form action using jQuery.

867 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

17 Experts available now in Live!

Get 1:1 Help Now