<?php

Interface Product_Type {
    public function get_all();
    
    //skipped other function definitions to keep this example simple
}
 
Class Shoe implements Product_Type {
    public function get_all() { return DB::table('shoes')->all(); }
}
 
Class Apparel implements Product_Type {
    public function get_all() { return DB::table('apparels')->all(); }
}
 
Class Mobiles implements Product_Type {
    public function get_all() { return DB::table('mobiles')->all(); }
}


// STD USAGE - BAD USAGE
if( $product_type == 'shoes' ) {
    $product_obj = new Shoe();
}
elseif( $product_type == 'apparels' ) {
    $product_obj = new Shoe();
}
elseif( $product_type == 'mobiles' ) {
    $product_obj = new Mobile();
}
 
$all_products = $product_obj->get_all();


// GOOD USAGE
Class Listing {
    private $product_type;
    
    public function set_product_type($product_type) {
        if( $product_type == 'shoes' ) {
            $this->product_type = new Shoe();
        }
        elseif( $product_type == 'apparels' ) {
            $this->product_type = new Apparels();
        }
        elseif( $product_type == 'mobiles' ) {
            $this->product_type = new Mobile();
        }               
    }
    
    public function get_all() {
        return $this->product_type->get_all();
    }
}
 
$listing_obj = new Listing();
$listing_obj->set_product_type($product_type);
$all_products = $listing_obj->get_all();