Jump to content


Photo

Object Oriented PHP Question


  • Please log in to reply
7 replies to this topic

#1 KevinM1

KevinM1
  • Moderators
  • Snarkimus Prime
  • 5,248 posts

Posted 12 October 2006 - 05:31 PM

I wasn't sure if I should post this here or in PHP Help, so I hope I got it in the right place.

I was reading through my O'Reilly PHP reference and noticed that PHP has a pretty simplistic object oriented side to it.  Everything more or less made sense until I got to the keyword abstract.  I just can't see why I'd ever make an abstract class or abstract methods if they couldn't be fleshed out, especially given PHP's interfaces keyword, which, at least on the surface, basically does the same thing.  So, with all that said, when would using an abstract class be beneficial?  And what are the differences between that and interfaces?

Thanks :)
Using 'global' is a sign of doing it wrong

#2 Jenk

Jenk
  • Members
  • PipPipPip
  • Advanced Member
  • 778 posts

Posted 12 October 2006 - 05:37 PM

When building a set of classes that share common functionality, it's useful to bundle that functionality into one class. Sometimes that functionality is useless on it's own and must be extended to complete the class; thus abstract comes into play :)

Not every method has to be abstract within an abstract class, infact you don't have to have any abstract methods at all.

#3 KevinM1

KevinM1
  • Moderators
  • Snarkimus Prime
  • 5,248 posts

Posted 12 October 2006 - 05:41 PM

When building a set of classes that share common functionality, it's useful to bundle that functionality into one class. Sometimes that functionality is useless on it's own and must be extended to complete the class; thus abstract comes into play :)

Not every method has to be abstract within an abstract class, infact you don't have to have any abstract methods at all.


So how would an abstract class be 'connected' to a non-abstract class that needs the functionality the former has?
Using 'global' is a sign of doing it wrong

#4 Jenk

Jenk
  • Members
  • PipPipPip
  • Advanced Member
  • 778 posts

Posted 12 October 2006 - 08:26 PM

<?php

abstract class A
{
    public function fooBar()
    {
        echo 'Foobar!';
    }
}

class B extends A
{
}

$obj = new B;
$obj->fooBar();

?>


#5 KevinM1

KevinM1
  • Moderators
  • Snarkimus Prime
  • 5,248 posts

Posted 12 October 2006 - 10:02 PM

<?php

abstract class A
{
    public function fooBar()
    {
        echo 'Foobar!';
    }
}

class B extends A
{
}

$obj = new B;
$obj->fooBar();

?>


...duh, I should've known that.  :D

Thanks. :)
Using 'global' is a sign of doing it wrong

#6 redbullmarky

redbullmarky
  • Staff Alumni
  • Advanced Member
  • 2,863 posts
  • LocationBedfordshire, England

Posted 12 October 2006 - 10:11 PM

Jenk - am I right (at all) in thinking that many of these keywords such as 'abstract' and 'interface', etc are ways of forcing a particular behaviour/usage of a class, rather than actually doing anything functional?
"you have to keep pissing in the wind to learn how to keep your shoes dry..."

I say old chap, that is rather amusing!

#7 neylitalo

neylitalo
  • Staff Alumni
  • Advanced Member
  • 1,854 posts
  • LocationMichigan, USA

Posted 13 October 2006 - 12:33 AM

Jenk - am I right (at all) in thinking that many of these keywords such as 'abstract' and 'interface', etc are ways of forcing a particular behaviour/usage of a class, rather than actually doing anything functional?


Well, I'm not Jenk, but I'll jump in - You're absolutely right.

Abstract methods and classes are the basis for polymorphism - the idea that objects should be able to take on several different "identities" and behave differently according to the specific need. For example, we'll pretend we're creating a little RPG with several different character types. Some of these character types will be able to attack, but specifically how that is done depends entirely on the type of the character. For example, an archer will shoot arrows, a swordsman will (naturally) swing a sword, and an assassin will creep up in the shadows and silently make his mark. So, in the base character class, we would create an abstract method attack(). Then, in each character class with the ability to attack, we would over-ride that abstract method with specific instructions as to how that character should attack. The abstract keyword comes in handy in our example to make sure that character objects unable to attack can't call the attack() method. An abstract method NEEDS to be over-ridden to be used, but just because an abstract method is defined, it does not have to be extended. The abstract keyword doesn't force a specific behavior; rather, it ensures that a behavior is defined before it is used.

Interfaces, on the other hand, force a specific behavior. An interface is simply an easy way to make sure that a given object has certain properties and methods. We'll use a different example to demonstrate the uses of interfaces - a car. You have to be able to steer the car, so we'll create an interface requiring each and every car object to have methods that turn the car's wheels. Now, we can't just have steerLeft() and steerRight() methods in the base car class - remember, back in the "old days", cars had no power steering. It was purely a mechanical link. That's the difference, and the reason we have to make an interface to force the steering behavior instead of just creating universal steering methods. So, on the cars that don't have power steering, the steerLeft() and steerRight() methods will contain instructions to put the mechanical linkage into motion. The cars that have power steering will have methods that know how to "activate" the power steering and turn the car. Note that, unlike abstract methods, methods in an interface are REQUIRED in classes that implement that interface.

So, in summary, abstract methods are used to make sure that the method is extended before it is used, and interfaces are used to require the definition of certain methods and properties crucial to the object.
http://nealylitalo.net - My personal website, and home of The Netizen's Journal.

#8 Jenk

Jenk
  • Members
  • PipPipPip
  • Advanced Member
  • 778 posts

Posted 13 October 2006 - 08:24 AM

abstract classes are a mixture of common functionality and ensuring abstract methods are defined.

final classes and methods are the complete opposite, you cannot extend a final class and you cannot overload a final method.

interfaces simply define what access points (thus: interface) are required.

abstract and interface have a use in type hinting.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users