SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    Always learning viveknarula's Avatar
    Join Date
    Mar 2006
    Location
    INDIA
    Posts
    418
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question Abstract classes

    Hi,

    I am not much new in classes and the oops however still have to use the abstract classes. i know the theoretical version of the abstract classes and i can do this as well however i am not been able to find out an particular scenario of the abstract classes.

    Can anyone please let me know any particular scenario and it's benefit of using abstract classes in that particular scenario.

    Please help.

    Thanks !!

  2. #2
    Always learning viveknarula's Avatar
    Join Date
    Mar 2006
    Location
    INDIA
    Posts
    418
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Anyone please

  3. #3
    Theoretical Physics Student bronze trophy Jake Arkinstall's Avatar
    Join Date
    May 2006
    Location
    Lancaster University, UK
    Posts
    7,062
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    An abstract class is basically a class which acts like an interface, but it allows you to define base methods where a class doesn't include one.

    For example:
    PHP Code:
    <?php
    abstract class Food{
        private 
    $status 0;
        private 
    $Ingredients = array();
        public function 
    getIngredients(){
            return 
    $this->Ingredients;
        }
        public function 
    addIngredient(Ingredient $ingredient){
            
    $this->Ingredients[] = $ingredient;
        }
        abstract public function 
    bake();
    }
    class 
    Pizza extends Food{
        
        function 
    __Construct(){
            
    $this->addIngredient(new Ingredient('Cheese'));
            
    $this->addIngredient(new Ingredient('Tomato'));
        }
        function 
    addTopping(Ingredient $ingredient){
            
    $this->addIngredient($ingredient);
        }
        public function 
    bake(){
            
    $this->status++;
        }
    }
    class 
    Ingredient{
        public 
    $Name '';
        public function 
    __Construct($Name){
            
    $this->Name $Name;
        }
    }
    $pizza = new Pizza();
    $pizza->addTopping(new Ingredient('Pepperoni'));
    var_dump($pizza->getIngredients());
    Off Topic:

    Guess who's in the mood for a pepperoni pizza?


    So say you have the above setup. Any class extending Food already has base functions defined for them, and they MUST implement a bake function.

    It's basically a way of making child classes do what they are there for.
    Jake Arkinstall
    "Sometimes you don't need to reinvent the wheel;
    Sometimes its enough to make that wheel more rounded"-Molona

  4. #4
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    It is used in the TemplateMethod pattern.

    Heres a tutorial in PHP5s Template Method which gives an example.

    The template method uses an abstract class to really blueprint the core ( or "primitive" ) jobs a class must do - but doesn't know or care how it is implemented.

    Concrete implementations can contain their own algorithms, or, can be allowed to activate "hooks" in the abstract class.

    Its incredibly easy to use. I made a series of Uploaders which i now use everywhere.

    It banished for me any questions I similarly had about abstract classes.

    Good luck with it.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •