Abstract class

Please explain why using abstract class? And why extending abstract class istead of extending the parent class directly?

Because the child class is what tells what the object is and the parent just give you the abstract class. Here’s an example that I did after someone else explained it to me (I think I made some modifications?).

<?php
abstract class Animal {

	// This class doesn't know what type of animal it is
	// child classes are supposed to implement it
	public abstract function getAnimalType();

}

abstract class Cat extends Animal {
  public abstract function breedCountry();
	// this class, and any child class, is a cat
	public function getAnimalType() {
		return "Cat";
	}

}

class Balinese extends Cat {
	
	public function breedCountry() {
		echo "United States";
	}
}

abstract class Dog extends Animal {

	// dogs can bark but not all dogs bark the same way
	public abstract function bark();

	// this class, and any child class, is a dog
	public function getAnimalType() {
		return "Dog";
	}

}

class EnglishDog extends Dog {

	public function bark() {
		echo "Woof";
	}

}

class ItalianDog extends Dog {

	public function bark() {
		echo "Bau";
	}

}

class SpanishDog extends Dog {

	public function bark() {
		echo "Guau";
	}

}

$dog1 = new EnglishDog();

$dog1->bark();
echo '<br>';
echo $dog1->getAnimalType();
echo '<br>';

$cat1 = new Balinese();
$cat1->breedCountry();
echo "<br>";
echo $cat1->getAnimalType();

One thing nice about an abstract class over an interface is that you can layer them and have other methods in them as long as you have it’s abstract itself. (I hope I explained the clearly?)

Thanks.
Please take a quick look at these 5 links and tell me why /Service/People is extending AbstractService instead of just extending Api.php?

https://github.com/DeskPRO/deskpro-api-php/blob/master/src/DeskPRO/Service/People.php

https://github.com/DeskPRO/deskpro-api-php/blob/master/examples/people/People.md

https://github.com/DeskPRO/deskpro-api-php/blob/master/examples/misc/exchange_for_token.php

Only a guess but I would say that people could have different criteria, by that I mean not all people are agents for example? Like I said only a guess.

Here https://github.com/DeskPRO/deskpro-api-php/blob/master/src/DeskPRO/Service/People.php
$interface is Api object in AbastractService constructor. So why clearSessions in this class calls “call” method of Api, via $interface but other methods in this class calls “call” method directly from Api?

Not all relationships between objects should be based on inheritance, and in fact composition is often preferable. In this case, think about the People service - could you say that the People Service is an API? Not really, but it does use an API, so composition makes more sense, passing an API object into the service via the constructor.

So why have an abstract Service class? Making it abstract prevents the base class itself from being instantiated - it isn’t useful unless it is extended with additional functionality for each specific service.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.