I write below in a single php file.
<?php
interface people
{
public function take($s);
}
class engineer extends people
{
public function take($s){
echo $s;
}
}
?>
The people is an interface, the engineer extends people.
But when I run this code, the error:
Fatal error: Class engineer cannot extend from interface people in E:\php5\Mywwwroot\b.php on line 12
What's happened? My PHP version is 5.4.
You implement interfaces and extend classes:
<?php
interface people
{
public function take($s);
}
class engineer implements people
{
public function take($s){
echo $s;
}
}
?>
extends is for extending another class.
For interfaces, you need to use implements instead.
(An interface can extend another interface, though)
Depends on what you want, it could be:
class extends aClass
class implements anInterface
interface extends anInterface
You can extend only one class/interface and implement many interfaces. You can extend interface to another interface, e.g. interface DieselEngineInterface extends EngineInterface.
Also want to note a comment, now that you can have class and interface hierarchy, you need to know when to use them.
Related
I wonder if it is possible to not implement a method coming from an interface and let child class do it.
For example :
abstract class Foo implements Bar
{
public abstract methodFromBar();
}
And then :
class SubFoo extends Foo
{
public methodFromBar()
{
// Implementation...
}
}
The idea behind this is to simplify development and just specifying that the subclass extends from the main class instead of writing again that the subclass implements the interface.
You don't need to mention the interface method in the parent class at all. If it doesn't implement the interfaces listed, then PHP will require that the subclass fulfills the contract instead. This will work fine:
interface Bar
{
public function methodFromBar();
}
abstract class Foo implements Bar
{
}
class SubFoo extends Foo
{
public function methodFromBar()
{
echo 'Hello world';
}
}
$subFoo = (new SubFoo)->methodFromBar();
// Hello world
See https://eval.in/1016337
If the subclass does not implement the method, you'll receive a message along the lines of
Fatal error: Class SubFoo contains 1 abstract method and must therefore be declared abstract or implement the remaining methods (Bar::methodFromBar)
Whether or not you think it's a good idea for an abstract class to implement an interface is probably a discussion for another site.
I'm facing the following issue in PHPStorm 9:
Say I have an interface FieldInterface that has some methods:
namespace Acme;
interface FieldInterface {
public function methodA();
public function methodB();
}
then I have an abstract class that implements base functionality of the interface. That abstract class has the user to implement certain methods, let's say it's methodB in our example:
namespace Acme;
abstract class AbstractField implements FieldInterface {
public function methodA() {
// implement methodA
}
public abstract function methodB(); // have the user implement it
}
And finally I have some ready-to-use class StringField:
namespace Acme;
class StringField extends AbstractField {
public function methodB() {
// implement methodB
}
}
At this point everything's going well. But if I add new method in the FieldInterface, PHPStorm does not say that anything is wrong with AbstractField while it's obvious that I should add public abstract function newMethod(); in there. However, it spots the error in StringField class instead.
It could be understood from the point that abstract classes are made for the purpose of extention, but usually you extend the abstract class rather than implement underlying interface. The whole meaning of making abstract class is to save user's time for implementing the interface. So why PHPStorm forces me to implement interface in concrete class rather than forcing me to implement it in abstract class that is explicitly implements the interface.
So I wonder if it is a bug in PHPStorm, or maybe it's done on purpose. Either way, is there any workaround?
That's how it should be, showing an error in the abstract class would be wrong.
In fact, public abstract function methodB(); is redundant because the abstract class already "inherits" this abstract method from the interface as it does not implement it.
The only workaround is to make AbstractField not abstract.
interface myI
{
public function myF();
}
class myTest implements myI
{
public function myF()
{
echo 'blah';
}
}
class myTest2 extends myTest
{
}
Above is a simple Interface requiring classes to implement the myF function. Now, if a class implements that interface, then all other inheritance of that class won't be require to implement it, right?
I tested:
$lists = new myTest2();
if($lists instanceof myI){
echo 'yes' . "<br />";
}
And it outputted yes.
Then I changed
Then I changed
class myTest2 extends myTest
to
class myTest2 extends myTest implements myI
and it still outputted 'yes', even though myTest2 did not implement the myF function.
Why is that?
It's extending the base class which already implements the interface and defines the method.
It is because your parent class myTest covers implementation of interface. You can overwrite implementation in myTest2.
Because myTest2 inherits myF from myTest. Implementing the interface, myTest2 already has the function inherited from myTest, so it is in check with the interface's requirements.
I am studying the differences between Abstract and Interface and I read some sentence
saying
A child class can only extend a single abstract (or any other) class,
whereas an interface can extend or a class can implement multiple
other interfaces.
I understand when he says, “A child class can only extend a single abstract (or any other) class,” he means:
class first
{
public function Search()
{
return 'Hellow';
}
}
abstract class first2 extends first
{
}
class second extends first2
{
}
$ob = new second();
echo $ob->Search();
However, I didn’t understand the rest of his sentence, where he says, “whereas an interface can extend or a class can implement multiple other interfaces.”
Could someone please explain his last sentence and add a code example?
Thank you all and have a nice day.
You can implement more than one interface
interface C {
public function method1();
}
interface D {
public function method2();
}
class A implements C,D {
//implement from interface C
public function method1() {
}
//implement from interface D
public function method2() {
}
}
Here you will need implement methods from interface C and D. You can also extend interfaces within interfaces, like normal classes.
interface D extends C{}
It's useful when well you need some common methods. so you write "schema" into interface what methods you are expecting from base class to be implemented.
While abstract is single extended class, you canot create instance for it, only extend. It's useful when you want have some base class with common functionality or abstract methods what should be implemented later.
More you can always read at php.net - interfaces
I have something like :
interface IProduct { }
class Product implements IProduct { }
class SpecificProduct extends Product { }
I need to have a interface, that extends IProduct, for my SpecificProduct.
Can I just do something like :
interface ISpecificProduct { }
class SpecificProduct extends Product implements ISpecificProduct { }
or will it overwrite the IProduct interface ?
This works in PHP, this way, just go through:
class SpecificProduct as it extends Product it implements IProduct already. As it implements ISpecificProduct as well, it has both interfaces.
SpecificProduct now has two interfaces then:
IProduct
ISpecificProduct
Yes, you can do that, and no, it will not "overwrite" IProduct, it adds to it.
In case you are using the ISpecificProduct interface somewhere and would like to know about the methods in IProduct, ISpecificProduct can also extend IProduct:
interface ISpecificProduct extends IProduct
In this case, SpecificProduct gains knowledge of the method signatures from IProduct in two ways.
See PHP documentation on interfaces for more information.
As a side note, if you have a matching interface for every concrete class, there is probably something inefficient or unnecessary going on in your object design.