Interface and its inheritances - php

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.

Related

Declaring abstract the method implemented from an interface

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.

How to impose contract to subclass from parent class implementation

If I have an interface and I want the functions in my interface to be implemented in my sub class but the parent class must implement the interface. And Sub class extends Parent Class.
Here is what I want.
interface MyInterface {
public function find();
}
class B implements MyInterface {
}
class A extends B {
}
But an error is thrown saying find() function must be added in class B.
Anyone who could enlighten me on what am I doing wrong? thanks!
Interface is a structure, which contains a set of fields and methods that have to be implemented in every class that implements this interface. If your B class implements MyInterface it means it has to implement all of its methods/fields.
interface MyInterface
{
public function find();
}
class B implements MyInterface {
public function find()
{
echo "Hello world";
}
}
Does A class have to implement this method too? I'll leave it as your homework.

PHPStorm 9 inspection of class inheritance works unexpectable

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.

How to use reflection as callback in php

I have one class
Class Mainclass{}
And another class which is
Class Childclass extends Mainclass{}
Now i want to write callback in Mainclass which check if child class found with method, merge value and return?
How can i achieve using reflection?
One easy way would be to create an interface containing that method
interface XYZ
{
public function myMethod();
}
And make your child class implement it
class Childclass extends Mainclass implements XYZ
{
public function myMethod()
{
//actual implementation
}
}
Afterwards, your main class can easily check if it is implementing that interface:
class Mainclass
{
public function whatever()
{
if ($this instanceof XYZ)
{
$this->myMethod();
}
}
}
Now, I'm fairly sure it would work but I really think this is bad design: a parent class should never depend on the implementation of its child classes. However since I don't know the context in which you're working, I'll leave this here and hope it helps you anyway.

PHP can't extend from interface?

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.

Categories