SitePoint Sponsor

User Tag List

Results 1 to 9 of 9
  1. #1
    SitePoint Addict
    Join Date
    Jan 2007
    Posts
    323
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Easy OOP question?

    Lets say we create two classes. An Articles class, and an Article class.

    I am not sure what makes more sense in these situations/examples below?

    $articles = new Articles();

    $articles->add(.....);
    $articles->delete(....);

    Or if we do...

    $article = new Article(45); // 45 is the article ID?
    $article->delete();

    Or...

    $article = new Article();
    $article->Create(......);

    Sorry... a general example would be great. We are looking at creating some general classes for adding/removing/editing some items. So we will have collections of items, and single items, whether they be articles, reviews, or so on.

  2. #2
    rajug.replace('Raju Gautam'); bronze trophy Raju Gautam's Avatar
    Join Date
    Oct 2006
    Location
    Kathmandu, Nepal
    Posts
    4,013
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I could not get what exactly you want. So what do you want from here to reply? Please be more descriptive. Do you want to create those classes or what?
    Mistakes are proof that you are trying.....
    ------------------------------------------------------------------------
    PSD to HTML - SlicingArt.com | Personal Blog | ZCE - PHP 5

  3. #3
    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)
    If your question is about naming, it might depend upon many other things.

    Do you expect to have methods like :

    collectAllByDate(4);
    collectIntro(8);

    and if your articles are in a database, how are you passing that db connection into the class - or does the class own a brand new connection?

    You may well end up with an Article class which just manages the CRUD on a single row (look at ActiveRecord ), and Articles which manages multiple rows, probably read only.

  4. #4
    SitePoint Addict
    Join Date
    Jan 2007
    Location
    Romania
    Posts
    203
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by atDev View Post
    Lets say we create two classes. An Articles class, and an Article class.

    I am not sure what makes more sense in these situations/examples below?

    $articles = new Articles();

    $articles->add(.....);
    $articles->delete(....);

    Or if we do...

    $article = new Article(45); // 45 is the article ID?
    $article->delete();

    Or...

    $article = new Article();
    $article->Create(......);

    Sorry... a general example would be great. We are looking at creating some general classes for adding/removing/editing some items. So we will have collections of items, and single items, whether they be articles, reviews, or so on.
    If you want to use new Article(45) look over Constructors and Destructors.

  5. #5
    SitePoint Wizard stereofrog's Avatar
    Join Date
    Apr 2004
    Location
    germany
    Posts
    4,324
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by atDev View Post
    Lets say we create two classes. An Articles class, and an Article class.

    I am not sure what makes more sense in these situations/examples below?

    $articles = new Articles();

    $articles->add(.....);
    $articles->delete(....);

    Or if we do...

    $article = new Article(45); // 45 is the article ID?
    $article->delete();

    Or...

    $article = new Article();
    $article->Create(......);

    Sorry... a general example would be great. We are looking at creating some general classes for adding/removing/editing some items. So we will have collections of items, and single items, whether they be articles, reviews, or so on.
    What you're asking about is essentially the difference between Active Record (=Article->create) and Table Data Gateway (=Articles->add) patterns.

    http://www.martinfowler.com/eaaCatal...iveRecord.html
    http://www.martinfowler.com/eaaCatal...taGateway.html

    Both have their up and downsides. TDG is relatively simpler to program and works better for smaller applications.

  6. #6
    SitePoint Addict
    Join Date
    Sep 2006
    Posts
    219
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi,

    If I understand you correctly, you want to know if it's best to create an article in the Article Model or The Articles Model??

    There are many Data related design patterns, but to me it looks like you are looking at Table Data Gateway and Row Data Gateway.

    The difference between the 2 is that the Row Data Gateway deals with an individual row (or article in your instance) and the Table Data Gateway deals with many rows (or articles)..

    Read up on these 2 patterns, then seperate the methods accordingly - If you're dealing with 1 Article (insert, update, delete) then the responsibility lies with the Article Class (Row Data Gateway), If you are dealing with operations on many Articles (list, findByTitle, deleteAll) then it should be in the Articles Class (Table Data Gateway).

    There are also other patterns that you might want to look at.... ActiveRecord, MetaDataMapper etc..

    Hope that helps.

    Dan

  7. #7
    SitePoint Addict
    Join Date
    Jan 2007
    Posts
    323
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Read up on these 2 patterns, then seperate the methods accordingly - If you're dealing with 1 Article (insert, update, delete) then the responsibility lies with the Article Class (Row Data Gateway), If you are dealing with operations on many Articles (list, findByTitle, deleteAll) then it should be in the Articles Class (Table Data Gateway).
    Ok but then the question is, when do these two classes interact? ever?

  8. #8
    SitePoint Addict
    Join Date
    Jan 2007
    Posts
    323
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Any help?

  9. #9
    SitePoint Addict webaddictz's Avatar
    Join Date
    Feb 2006
    Location
    Netherlands
    Posts
    295
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by atDev View Post
    Ok but then the question is, when do these two classes interact? ever?
    Table Data Gateway and ActiveRecord (or Row Data Gateway) are no classes. These are Design patterns, global solutions to common problems. It's highly unusual to implement both TDG *and* ActiveRecord (well, that's the way I see 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
  •