Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 11-30-2009, 09:09 PM   PM User | #1
johnnnn
New Coder

 
Join Date: May 2009
Location: Pennsylvania, United States
Posts: 54
Thanks: 16
Thanked 0 Times in 0 Posts
johnnnn is an unknown quantity at this point
Question OOP -> abstract classes.

Today, I've been brushing up on my coding skills, so I decided to review the PHP manual (Object oriented Programming). Most of it came very easily to me, but I just do not get the point of abstract classes.

Heres what I know about them so far:
  1. Any member declared abstract, the class itself must be declared as abstract
  2. You can not instantiate an abstract class

Basically, from my understanding, abstract classes just hold methods and properties that MUST be defined in the child class is this correct? If not, can someone please explain to me what the point of abstract classes are and how to use them?

Any input is greatly appreciated, Thanks!
johnnnn is offline   Reply With Quote
Old 11-30-2009, 09:18 PM   PM User | #2
oracleguy
Rockstar Coder


 
Join Date: Jun 2002
Location: USA
Posts: 9,043
Thanks: 1
Thanked 322 Times in 318 Posts
oracleguy is a jewel in the roughoracleguy is a jewel in the roughoracleguy is a jewel in the rough
Abstract base classes (ABCs) have a couple primary uses:

1) As an interface, you can define a generic interface that derived classes must implement. That lets you treat multiple similar but different objects the same. Especially since not all languages have a specific interface construct.

2) It allows you to not define all the functions, so if you build a base class for say shapes and the base class can have the functions common to all types of shapes. But you could have a draw function that is abstract since that would be specific to the shape.
__________________
OracleGuy
oracleguy is offline   Reply With Quote
Users who have thanked oracleguy for this post:
johnnnn (11-30-2009)
Old 12-01-2009, 12:36 AM   PM User | #3
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,635
Thanks: 4
Thanked 2,448 Times in 2,417 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
Best phrase I can think of is minimal.
While interfaces IMO have exceedingly more power than an extension ever will, abstracts are the perfect minimal implementation of an interface. If you have an interface with say a public function loadINI() method on it, and every class uses the same INI file, then it makes sense to use an abstract to provide this minimal implementation detail instead of having the exact method on every class implementing it. This is especially true if it makes use of a method that has unknown implementation at the time, allowing you're class to call this method; if there is no call to a required method, a concrete adapter may make more sense (an adapter provides non-implementation by throwing exceptions or providing no implementation details for the concrete class). You may also have a base class that does not implement a provided interface and still defines a method, and then later extend it and implement this method. That will also work, though the base is not itself considered a typeof the given interface.
Note that the above can be easily modified to create an unrecognized bug in PHP (as in, the PHP team threw out my bug report indicating normal behaviour) when it is not the case. If you use this class structure:
PHP Code:
<?php

interface IInterface
{
    public function 
test();
}

class 
Grandfather
{
    private function 
__construct()
    {
    }

    private function 
test()
    {
        
printf("%s\n"__METHOD__);
    }
}

class 
Father extends Grandfather implements IInterface
{
    public function 
__construct()
    {
        
printf("%s\n"__METHOD__);
    }

    public function 
test()
    {
        
printf("%s\n"__METHOD__);
    }
}

class 
Son extends Father
{
    protected function 
__construct()
    {
        
parent::__construct();
        
printf("%s\n"__METHOD__);
    }

    public static function 
createInstance()
    {
        return new 
self;
    }

    private function 
test()
    {
        
parent::test();
        
printf("%s\n"__METHOD__);
    }
}


?>
Grandfather must not implement the interface and declare the same method signature as private.
And attempt to use the test method on an instance of Son, it will succeed and throw an illegal access error. This is incorrect as the error should be thrown when the linking occurs for the Son to Father - a child cannot demote the access level of a parent. This bug chews on me so much.
__________________
As of PHP 5.5, the MySQL library has been officially deprecated. It is recommended to move to either MySQLi or PDO libraries for your mysql connectivity. See here for help choosing which interface you prefer: http://php.net/manual/en/mysqlinfo.api.choosing.php
Fou-Lu is offline   Reply With Quote
Old 12-01-2009, 04:20 PM   PM User | #4
oracleguy
Rockstar Coder


 
Join Date: Jun 2002
Location: USA
Posts: 9,043
Thanks: 1
Thanked 322 Times in 318 Posts
oracleguy is a jewel in the roughoracleguy is a jewel in the roughoracleguy is a jewel in the rough
It should also be said like all things relating to OOP, it is very easy to over do it. ABCs aren't needed all the time, you have to use care and consideration when designing your objects.
__________________
OracleGuy
oracleguy is offline   Reply With Quote
Reply

Bookmarks

Tags
abstract, classes, oop, php

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 05:19 PM.


Advertisement
Log in to turn off these ads.