Multi-level inheritance in PHP classes - php

Lets say I have this code:
class A {
...
}
class B extends A {
...
}
class C extends B {
...
}
$c = new C();
$c->getMethodOrPropertyFromB()->getMethodOrPropertyFromA();
Other than a bad architecture or bad design will this have any impact on PHP / Webserver (Apache/Nginx) performance when the script executes?
If this is not recommended to have such multi-level in PHP classes, can you explain why?
Note: in addition to the answers I've got I will let this here which is helpful as well

My initial thought was that this may not be good for inheritance but after testing it seems okay. However, there are other means of accomplishing this you could be aware of.
Abstract classes or Interfaces may make sense.
Abstract classes are just like other classes but they cannot be instantiated. There are also abstract methods which must be implemented by concrete classes.
abstract class A {
//You can also have abstract methods
abstract public function doFoo();
abstract public function doBar($when);
//Also implemented method which when
//called unless overridden will use this logic
public function sayHi(){
echo "hi";
}
}
Now this class can choose to implement the abstract methods or not also adding any further logic needed by it.
abstract class B extends A {
public function doFoo(){
//Some code
}
abstract public function doFooBar();
public function sayBye(){
echo "bye";
}
}
This is a concrete class and all abstract methods must be implemented here if not already ones that are implemented can be overridden yet again.
class C extends B {
public function doFoo(){
//Some different code
}
public function doBar($when){
//Some code
}
public function doFooBar(){
//Some code
}
//do not need sayHi() and sayBye() but they will be available.
}
Interface in a simple and crude way is a bag of methods. You are simply telling the developer if you are going to use this implement these. These methods are not declared as abstract but cannot be implemented in the interface.
interface iA {
public function doFoo();
public function doBar();
}
An interface can be extended by other interface which is just adding more methods to the interface
interface iB extends iA {
public function doFooBar();
}
interface iC {
public function doAnything();
}
And implemented by classes
class A implements iA{
public function doFoo(){
//Some Code
}
public function doBar(){
//Some Code
}
}
class B implements iB{
public function doFoo(){
//Some Code
}
public function doBar(){
//Some Code
}
public function doFooBar(){
//Some Code
}
}
The added advantage of interfaces is that a class or abstract can implement more then one
abstract class C implements iA, iC {
public function doFoo(){
//Some Code
}
}
class D extends C {
//get doFoo() from C implementation and must implement the remaining...
public function doBar(){
//Some Code
}
public function doAnything(){
//Some Code
}
}

This seems perfectly fine. PHP only support single inheritance - so you can only inherit from one class.
If you need more functionality in a class but cannot get the functality in a parent class you can also consider using traits. Traits try to solve the single inheritance problem - even if it's not a problem per se.
If you properly build your classes you get a nice chain of inheritance which does not have any bad influences onto Apache/Nginx.

Related

When do we should use abstract function or normal function in base class in PHP?

So I have a question about the difference between "when we should declare normal function" and "when we should declare abstract function" in base class. Look at my example.
In the abstract class:
abstract class Birds {
abstract public function fly();
}
class Swallow extends Birds {
public function fly() {
// This function override fly function in Birds class
echo "Implement fly function in Swallow class";
}
}
In the normal class:
class Birds {
public function fly() {
echo "Implement fly function in Birds class";
}
}
class Swallow extends Birds {
public function fly() {
// This function override fly function in Birds class
echo "Implement fly function in Swallow class";
}
}
What you can see. The fly function in Swallow class is inherited by Birds class (in all cases). They are a same thing. So I'm embarrassed and I dont know when we should declare abstract function in base class?
Thanks for your help!
Abstract functions are actually only an interface. E.g. there's no difference in your example between abstract class and if it would be an interface (that's because there's only abstract method).
//abstract class
abstract class Birds {
abstract public function fly();
}
//interface
interface Birds {
public function fly();
}
That's because abstract methods have the same purpose that interface's method. When you somewhere else create a function (or method or a class or another interface etc.), and you will require any Birds instance, you will be sure you have that abstract method avaiable, although it was not implemented in Birds.
public function sendBirdToSpace(Birds $bird) { //no matter what Bird subclass
$bird->fly(); //you're sure this method is avaiable
}
Also usually you will have more than one child class. When it comes to that, it's getting more clear about abstract method.
It's actually pretty simple. Should Birds have a default behaviour implementation of flying? That's all. If every bird should can fly, but there's no default method - make it abstract.
Taken from PHP OOP Class Abstraction:
When inheriting from an abstract class, all methods marked abstract in
the parent's class declaration must be defined by the child;
additionally, these methods must be defined with the same (or a less
restricted) visibility. For example, if the abstract method is defined
as protected, the function implementation must be defined as either
protected or public, but not private.
This is essentially saying that your Swallow class has to inherit (have) the fly() method in it if it extends the Bird class based off of its abstract definition.
The general rule of thumb when harnessing abstract methods is when you want normality among classes.
Take the following for example:
Class Car {
abstract public function make();
abstract public function model();
}
This is our "base" class. Anything that extends from this has to specify the make() & model() methods.
Class Tesla extends Car {
public function make() {}
public function mmodel() {}
}
As you see above, our Tesla class has the required methods within. If you do not include these methods, you'll have PHP errors thrown.
Note
If you're exploring this option of "container" like development, then I'd suggest that you have a good look at PHP OOP Object Interfaces too, well worth it!

Difference between abstract class extends and normal class extends

Is there any clear difference why to use abstract for extends if we can do same in with the normal class excepts it doesnt provide the contract for eg.
abstract class Survivalneeds {
abstract public function eat(); // everyone eats but different foods which would probably work as contract
public function breathe() {
// everyone inhale o2 exhale co2 only for animals
}
}
Now
class human extends Survivalneeds {
protected function eat() {
//sometimes eat goat
// contract
}
breathe()// already extending having same functionality inhale o2 and exhale co2
}
class goat extends Survivalneeds{
protected function eat() {
//wee eat greens
// contract
}
breathe()// already extending having same functionality inhale o2 and exhale co2
}
Now the same functionality can be granted by normal class by extending except the contract method and for contract we could use interface also.
What you are saying its correct inheritance works in both cases but the idea of an Abstract class is that its some common logic shared by x classes that extend this functionality but that is not instantiable by it self because it doesn't make sense (maybe you want only to have types of cars in your system but not a generic car that doesn't have a brand)
Also if you will use regular class and interface you will be forced to create stub in a class in order to follow the contract. So you will be able to create the instance of the class. And just imagine you will use this common function in your upper class.
interface Crashable{
function crash();
}
class Car implements Crashable{
function crash(){}
function getCrashParams(){
return $this->crash();
}
}
class Volvo extends Car{
function crash(){
parent::crash(); // will be OK that it's not right
//.. specific params
return $params;
}
}
class Saab{
function crash(){
//.. specific params
return $params;
}
}
$car = new Car(); // will be ok, that it's not right
//getCrashParams() function in a Car will use the local version of the crash() and not the function of it's child that will kill the data flow
You should use an interface whenever you have a need for a contract. You should use abstract class in case there's a common functionality for some simmilar classes and you don't want to repeat the code (DRY :). Of course, it is always better to use composition, but this is not the time for this discussion :)
The problem with your code (with Survivalneeds class) is the fact the class from one side is responsible for the contract (breathe and eat methods) and from another is responsible for providing common functionality. You could change your code in following way:
interface Survivor {
public function eat();
public function breathe();
}
abstract class Survivalneeds implements Survivor {
public function breathe() {
// method's body
}
}
With such implementation responsibilities are splitted. Also it is clear that all classes that will extend Survivalneeds will need to as well fulfill Survivor contract.

PHP, OOP, Different formulation

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

Parent class inheritance of child class functions

I'm writing a power plugin for wordpress that basically supplies a bunch of functions to make development easier.
Don't worry about the wp stuff though, this is a PHP question. I have one master class 'my_wp_funcs', and a few other large classes that do different things, which I've written separately and work on their own, for example: insert a new post.
I would like to be able to use this syntax:
$wpfuncs = new funcs;
$wpfuncs->createpost($args);
$wpfuncs->addimage();
where createpost class extends funcs class, along with other classes that extend funcs too.
I've been reading up on abstraction, but am getting continual errors. Here's a trimmer version of what I have:
<?php
$wpfuncs = new funcs;
$wpfuncs->createpost($args);
abstract class funcs
{
abstract protected function createpost();
public function createpost($args){
$tool = new $this->boss_posttype('derp', 'derps');
}
}
class createpost extends funcs{
public function __construct(){
//do stuff
}
}
Cheers for any help!
You can't define the method as abstract in the abstract class and then define it for real in the same class. You also can't call the abstract class directly.
You probably want something like this:
abstract class funcs
{
abstract public function createpost($args);
}
class myFuncs {
public function createpost($args){
$tool = new $this->boss_posttype('derp', 'derps');
// do other stuff
}
}
$wpfuncs = new myFuncs();
$wpfuncs->createpost($args);
Note that your implementation goes in your own class, and that implementation has to match your abstract definition. (they both have to be public and they have to accept the same arguments)

Calling a child object in the parent object

I want to know if I can create a child object in a parent object an use it...
I mean is it posible?
Is it a good idea?
What do I have to care if I do so?
I am using the child classes on their own and i want to use them in a private function of the parent class as well.
thanks
here some source to imagine what I am meaning:
class A{
private $child_class1;
private $child_class2;
private function somefunction(){
$this->child_class1 = new B();
$this->child_class1->do_something();
$this->child_class2 = new C();
$this->child_class2->do_something();
}
}
class B extends A{
public function do_something(){
...
}
}
class C extends A{
public function do_something(){
...
}
}
You could use abstract methods to delegate certain actions to derived classes:
class A {
public function __construct() {
$this->do_something();
}
protected abstract function do_something();
}
class B extends A {
protected function do_something() {
// ...
}
}
It is not certainly a good idea to do so. It depends on what you want to achieve, and why. Creating derived classes in the parents constructor (like your previous example stated) is impossible. The language might allow it, but it will lead to an endless loop of instantiation. You will definitely need to explain why you are doing this. Otherwise nobody will be able to help you sufficiently.
This seems to be like a bad idea IMHO - it's going to require high maintenance and you are creating some tight couplings between classes.
i would recommend creating an abstract function in the parent that each of the children will implement with it's own logic.
[EDIT] since you are trying to iterate over all child objects i would recommend to create a base class that handles all the logic that needs to be implemented to all children, and override it in each of the child classes that need additional logic, and call the parent function inside it.
I think the big question should be why you would want it. I cannot come up with a solid design that would have a class extending something (the default example could be furniture, so for instance GardenChair extending Chair) be available in the parent class.
The whole idea is that it should be the other way around.
If you want to call the 'do_something', you should make an instance of the child, and let it call itself. If you need to enforce the do_something, try it like this:
public abstract class A{
public abstract funcion do_something();
public function __constructor(){
$this->do_something();
}
}
public class B extends A{
public funcion do_something(){
...
}
}
public class C extends A{
public funcion do_something(){
...
}
}

Categories