www.webdeveloper.com
Results 1 to 5 of 5

Thread: OOP Question - Extend vs New Class

  1. #1
    Join Date
    Dec 2010
    Posts
    232

    OOP Question - Extend vs New Class

    I'm just looking for opinions on my situation in relation to oop. I'm a beginner at the moment. Basically in my site I have files, photos and I also have messages.

    Therefore I currently have a "message" class and a "file" class. Both of these currently extend a class called "general" which has several methods, all of which both of these classes make use of hence the extension of the general class in each.

    However the both of these classes now need to make use of another method called "getAuthor" which I haven't included anywhere just yet. The thing is I also have another class called "photo" which DOES NOT extend the general class (yet) however it DOES need to make use of the "getAuthor" method.

    What is the best way of handling this situation? Perhaps by creating a new class called "Author" and instantiating that class inside the other classes wherever it is needed? The other option seems to be putting getAuthor in the general class and then extending the general class in the photo class but considering the fact that the photo class uses none of the other methods in the general class this seems bad practice?

  2. #2
    Join Date
    Jul 2003
    Location
    The City of Roses
    Posts
    2,503
    When deciding whether to use a class or extend a class, it can be helpful to ask whether the class "is a" something or whether it "has a" something. In the case of an author, both a message and a photo "has a" author, so I think you're on the right track with creating an Author class that both the Message and Photo classes can use.

    Ideally, once implemented, you might end up writing code this way:

    PHP Code:
    $author = new Author('Joe Shmoe');
    $message->setAuthor($author);

    $author = new Author('Jane Smith');
    $photo->setAuthor($author); 
    for(split(//,'))*))91:+9.*4:1A1+9,1))2*:..)))2*:31.-1)4131)1))2*:3)"'))
    {for(ord){$i+=$_&7;grep(vec($s,$i++,1)=1,1..($_>>3)-4);}}print"$s\n";

  3. #3
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    18,936
    What Jeff is (astutely) suggesting is often referred to as "composition", in case you want to try some searching on that topic. You inherit (extend) when class A "is a" class B, and you use composition when class A "uses" class B. A classic example of composition is passing a database object (such as a PDO or MySQLi object or one from your own custom class) to a data model object of some kind so that the model can access the database via the DB object.
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  4. #4
    Join Date
    Nov 2011
    Posts
    29
    I would put setAuthor in the general class and extend the photo class with general if that method is required for photos also. Essentially you want to put everything in general class that belongs to all three object classes.

  5. #5
    Join Date
    Dec 2010
    Posts
    232
    Thanks very much guys. That's very clear and now I understand it perfectly

    I suppose the best approach would be to make a general folder inside my class library folder containing all general classes that are used in many places and then instantiate certain classes as and when they are needed inside other classes i.e. "new General_Author()" etc.

    @faithless - Those were my original thoughts too however the photo class doesn't use any of the other methods in the general class so it seems bad practice because I'd be including a lot of methods that the class doesn't need to implement at all.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles