Jump to content

Recommended Posts

I'm still learning the OOP essentials, so please bear with me.

 

Is it fair to say that abstract classes and interfaces are just another abstraction layer above classes?  Or to put it another way, if a class is a blueprint for an object,

could an abstract class be thought of as a blueprint for a class?

Link to comment
https://forums.phpfreaks.com/topic/124708-abstract-classes-and-interfaces/
Share on other sites

Basically, yes.  All classes that implement an interface or abstract class would need to define the methods in the class or interface themselves.  This means that any class can be treated the same as long as it implements or extends the interface or class, because you'll know they have the same methods.

You'd really only need to abstract your classes if you have a bunch of related classes that handle their internal business logic differently, but are called the same way.  Ex:  User abstract class, Member, Admin, Moderator, etc.  The latter 3 can all do $user->something();, but each will do them differently.

Abstract classes (and interfaces) are typically used to define a "pattern" for any child classes.

e.g.

 

<?php
// Human - Abstract class
abstract class human {
// Retrieve name - must be implemented by all concrete children.
abstract public function getname();
abstract public function getage();
abstract public function hurl();

}

// Jane - Concrete class
class jane extends human {

public function getname(){
	return "jane";
}

public function getage(){
	return 23;
}

public function hurl(){
	echo "i can't throw i'm a girl!";
}

}

// John - Concrete class.
class john extends human {

public function getname(){
	return "john";
}

public function getage(){
	return 27;
}

public function hurl(){
	echo "John has throw(n) up...";
}

}

?>

 

An abstract class is used to define a concrete class. You CANNOT instantiate an abstract class (i.e. $class = new AbstractClass() ).

Any concrete children MUST implement any declared abstract functions.

 

Now when you come to use the child classes you know that they all maintain the same methods (but with different outcomes).

<?php

function testPerson(human $person){
  $person->hurl();
}

testPerson(new jane());
testPerson(new john());

?>

 

Because we know that jane's and john's both implement the hurl() function we can interchange them throughout our code, replacing one with the other at any time without worrying about having to modify our code for the new class type.

I don't believe a strategy pattern would overcome this issue Dan.

Normally I would consider throwing an exception, but in order to demonstrate what the OP asked for, which was a differentiation of classes into abstracts/interfaces, I believe i have covered the point well.

 

"Program to an implementation"

 

I'd be keen to here your strategy suggestion however, as I often believe there is inherent weakness in methods I implement that aren't "used" in all children.

 

If you are suggesting that you replace the hurl() method with a strategy then again you are faced with essentially the same problem. I shall demonstrate:

 

<?php
// Human - Abstract class
abstract class human {
protected $hurlStrategy;

// Retrieve name - must be implemented by all concrete children.
abstract public function getname();
abstract public function getage();

public function setHurlStrategy(strategy_hurl $hurl){
	$this->hurlStrategy = $hurl;
}

public function hurl(){
	$this->hurlStrategy->perform();
}
}

// Jane - Concrete class
class jane extends human {

public function __construct(){
	$this->setHurlStrategy(new strategy_hurl_girl());
}

public function getname(){
	return "jane";
}

public function getage(){
	return 23;
}

}

// John - Concrete class.
class john extends human {

public function __construct(){
	$this->setHurlStrategy(new strategy_hurl_boy());
}

public function getname(){
	return "john";
}

public function getage(){
	return 27;
}

}

abstract class strategy_hurl {
abstract public function perform();
}

class strategy_hurl_boy {
public function perform(){
	echo "throw(n) up";
}
}

class strategy_hurl_girl {
public function perform(){
	echo "i can't throw i'm a girl!";
}
}
?>

 

The calling script i included above still applies.

 

Jane still can't hurl...

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.