Hi all,

I am trying to lay out a plan for my classes, and havent godt the most experience in oop yet.

I am using an abstract class, to be able to have a more clear overview and keep the project easier to manage.

SO:
I have created two abstract classes and extended them, so the child classes implements the desired methods.

class concrete_product_views extends abstract_product_views
{
    public function display_index_products( $ProdID )
    {

    }

    public function display_product_category( $CatID, $limit = 5 )
    {

    }

    public function display_product( $ProdID )
    {

    }
}

NOW, inside this class I need to use methods from another class, which has got all the methods which defines the product carachteristics I am using for the display/output, such as: meassurements, price, thumbnails, specs, marketing text etc.

How can I use that other class inside the class above? I have allready extended it to use the abstract parent class.

Is it an "acceptable approach", to instantiate classes inside another class that is extended from an abstract class?

IS this a good/normal approach? See the first method - Do I need to instantiate that class inside EACH display method, or is there a better way? Can I implement it in the class somehow even thoug it is extended... so I dont need to do it in every display method? Its not the end of the world, Im just trying to optimize the classes, and write no more than I need :-)

class concrete_product_views extends abstract_product_views
{
    public function display_index_products( $ProdID )
    {
        // USE DATA FROM PRODUCT_DETAIL CLASS
        // WHICH IS EXTENDED FROM ABSTRACT_PRODUCT_DETAIL CLASS
        $details            = new product_details( $ProdID );
        $measurements       = $details->product_meassurements( $ProdID );
        $price              = $details->product_price( $ProdID );
        $specs              = $details->product_specs( $ProdID );
        $marketing_text     = $details->product_marketing_text( $ProdID );
        $key_selling_points = $details->key_selling_points( $ProdID );
        $thumbs             = $details->thumb_images( $ProdID );
    }

    public function display_product_category( $CatID, $limit = 5 )
    {

    }

    public function display_product( $ProdID )
    {

    }
}

Regards, Klemme

I think you are confusing interface and abstract class. You would implement an interface (roughly what you're doing, but another syntax) if you want to have generic function that would typically execute a commonly named, but specifically implemented method.

For example you would created an interface "productdisplays" that would have a declaration for a method display() and then several types of products that would each have there own display() method implemented.

Abstract classes on the other hand are classes that are not instanciated, but contain common logic. In the same situation as above, you would implement the display method in the abstact class.

So your abstraclass could be called "product" and you could derive a "specific procuct" and set its particular properties, code additional methods, or overwrite some that are coded in the abstract class.

You could have a property $color in your abstract class that is set to "black", and then code "greenproduct" and set the color to "green".

Hope this somehow clarifies. It's a bit complex material, but believe me, it's well worth your time to try master it.

Maybe you could tell us what it is you are trying to achieve.

In addition to PerplexedB's input, think of Abstract as a contract between the parent and child classes.

Abstract class have an abstract method without returning anything, for the purpose of allowing the child class do the returning of the output whatever it may be.

Example of abstract implementation, most commonly found in MVC design pattern. Say, we want to create a base controller for our application. We want to make sure that all public controllers abide by the contract of returning the abstract method called "index".

Abstract Class BaseController {



function __construct() {


}

//this is the contract declaration that must be fulfulled by all child classes

abstract function index();

}

Noticed the above usage of abstract. Abstract class with abstract method called index.

If we want to create a controller class for let say article to show articles stored in the database, we must fulfill the contract established or rather required by the parent class which is the BaseController.

Warning! Codes below is just a sample... it the modle class and view class has been omitted ONLY for the purpose of demonstrating the concept.

Example of a child class of the BaseController abstract class

class ArticleController extends BaseController{



public function __construct(){

    parent::__construct(); //must declare the parent construct inheritance
}

public function index(){

     return 'Something';

}

}

Again, the child class 'ArticleController' inherits the BaseController's constructor, and then implements or fulfill the contract responsibility for the parent's abstract method called index().

that's it for now... if I keep on blobbering about things that have not been seen, they will be deemed as useless and hyped, because no one will ever give it some considerations and good logical interpretations.

Member Avatar for diafol

WRT using functions from other classes. I'm a bit confused about the usage. Would dependency injection work for you, allowing separation of concerns (SoC)? I may have got the wrong end of the stick here.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.