SitePoint Sponsor

User Tag List

Results 1 to 25 of 25
  1. #1
    SitePoint Wizard
    Join Date
    Feb 2009
    Posts
    1,006
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question Inheritance and Composition between DbConnectionClass and ClassThatPerfomsQuerys

    Please:
    I DO understand the sense, when we read: That a car is an aggregation of engine, doors, Wheel…

    What I don’t understand is this much more concrete statement here:
    http://www.sitepoint.com/article/object-oriented-php/10/
    “The class performing the query aggregates the database connection class.”

    Because, according to this definitions:
    "An aggregation is a complex object composed of other objects."
    "An association is used when one object wants another object to perform a service for it."

    I must conclude that the relation between a DbConectionClass and the ClassThatPerformsTheQuerys seems to be more of an association then an aggregation.


    Specific Question:
    How should I relate my DbConnectionClass with the other classes that uses the ConnectionClass to perform querys?



    Thanks.
    Márcio

  2. #2
    SitePoint Wizard silver trophy kyberfabrikken's Avatar
    Join Date
    Jun 2004
    Location
    Copenhagen, Denmark
    Posts
    6,157
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Aggregation is often used loosely to mean that one object has a reference to another object. In UML, one can distinguish between aggregation and composition, while in implementation, they look the same. Association is is an even broader term, simply meaning that two objects relate somehow to each other (Be it through inheritance, composition, aggregation or a locally/temporarily scoped variable). You might find this explanation helpful. Don't fret too much over it - In implementation, the important distinction is between inheritance and composition.

    Quote Originally Posted by oikram View Post
    Specific Question:
    How should I relate my DbConnectionClass with the other classes that uses the ConnectionClass to perform querys?
    Composition - Not inheritance.

  3. #3
    SitePoint Wizard
    Join Date
    Feb 2009
    Posts
    1,006
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Composition for sure. But the question is more, witch one?
    The Aggregation type or the Association type?

    You are telling me that:
    In UML, one can distinguish between aggregation and composition, while in implementation, they look the same.
    And I'm actually trying to do a UML, but since on the implementation they look the same, should I just pick one?
    Update: No. I get composition. Still, witch type of composition?




    Thanks for the link.

    I'm reading this btw:
    "The Object-Oriented Thought Process"
    By Matt Weisfeld


    Regards,
    Márcio
    Last edited by oikram; Apr 7, 2009 at 10:17.

  4. #4
    SitePoint Addict
    Join Date
    Nov 2005
    Location
    Germany
    Posts
    235
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by kyberfabrikken View Post
    [...] In UML, one can distinguish between aggregation and composition, while in implementation, they look the same.
    I thought while being associated by composition one object has control over the live cycle of another object. So in an implementation of composition you're likely to find calls to construct() and destroy(). Aggregation on the other hand should require some injection, i.e. passing of objects.

  5. #5
    SitePoint Wizard silver trophy kyberfabrikken's Avatar
    Join Date
    Jun 2004
    Location
    Copenhagen, Denmark
    Posts
    6,157
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by FrlB View Post
    I thought while being associated by composition one object has control over the live cycle of another object. So in an implementation of composition you're likely to find calls to construct() and destroy(). Aggregation on the other hand should require some injection, i.e. passing of objects.
    Yeah, that's true. What I meant was that in both cases, the relationship is member variable. So the difference between aggregation and composition becomes the life-cycle management - Not how they are actually interacting between creation and destruction.

  6. #6
    SitePoint Wizard silver trophy kyberfabrikken's Avatar
    Join Date
    Jun 2004
    Location
    Copenhagen, Denmark
    Posts
    6,157
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by oikram View Post
    Composition for sure. But the question is more, witch one?
    The Aggregation type or the Association type?
    Well, I would say that relating a database connection class to query classes through composition, would be wrong. While the query couldn't really exist without the connection, this coupling would make the connection have knowledge about the query. I would say that the connection class belongs to an infrastructure layer, while the query belongs in the model layer. The infrastructure layer is more fundamental than the model layer, so it shouldn't know what builds on top of it. Thus, to preserve separation of layers, I wouldn't use composition in this case. If you want to manage the life-cycle in a structured way, you could use an external, generic component, such as a dependency container or registry.

  7. #7
    SitePoint Wizard
    Join Date
    Feb 2009
    Posts
    1,006
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks a lot for your comments. What could I say?
    I have read about DI implementations, and there are a butch of new concepts that I don't understand.
    It's out of my reach right now. I'm developing alone, with two weeks of PHP, and OOP... so... well... later... later...
    Otherwise I will loose myself on studies and I will end up implementing nothing. Since I always want to know, I will get better with time.

    For now, I stick with composition, and yes... I have to call it (I don't know how yet) for every single class that intends to use it.


    Thanks a lot for your time and tips, I'm really sorry, I feel like you are telling me, jump 3 meters, but I know I have legs for just some cms... At least for now.




    Regards,
    Márcio

    ps- I believe I will have a total of 12-15 different classes, so (I hope) it will not be a big problem to manage.

  8. #8
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2006
    Location
    Augusta, Georgia, United States
    Posts
    4,194
    Mentioned
    17 Post(s)
    Tagged
    4 Thread(s)
    The best thing you can do for yourself is to determine the exact requirements of the application then make decisions based on those requirements alone. If your just making decisions that satisfy the needs of one piece your bound to run into problems down the line unless your doing so in a highly abstract and generic fashion. However, this doesn't seem like the case in your situation. Only dealing with a Site and Client class is fine, but the real test is whether or not the methodology holds up when applied to everything else that will need to be managed.

  9. #9
    SitePoint Wizard
    Join Date
    Feb 2009
    Posts
    1,006
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks oddz. Well, I'm trying to create a UML before actually code things.
    But, in this case, I need to get a little sniff here and there, and figure, how can we implement those things. For example, how a database connection class will deal with other classes, so I can decide, after some reading, what the UML relation should be.

    I have made a physical diagram model of the mysql database and I was hopping to copy the table relation and transfer that relation to a class relation to.
    I have read that DAO does this...

    Isn't a good method?

    Regards,
    Márcio

  10. #10
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2006
    Location
    Augusta, Georgia, United States
    Posts
    4,194
    Mentioned
    17 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by oikram
    I have made a physical diagram model of the mysql database and I was hopping to copy the table relation and transfer that relation to a class relation to.
    I have read that DAO does this...

    Isn't a good method?
    Difficult to say without seeing a basic example of the implementation and interface.

    Quote Originally Posted by oikram
    But, in this case, I need to get a little sniff here and there, and figure, how can we implement those things. For example, how a database connection class will deal with other classes, so I can decide, after some reading, what the UML relation should be.
    The connection should be separate so either pass it to the constructor or the specific method. Not much else you could do besides use a registry. However, your still passing it indirectly.

    PHP Code:
    $client = new Client();
    $client->save($db); 
    PHP Code:
    $client = new Client($db);
    $client->save(); 
    PHP Code:
    $reigisry->db $db;
    $client = new Client();
    $client->save($reigisry); 
    PHP Code:
    Client::setConnection($db);

    $client1 = new Client();
    $client2 = new Client();

    $client1->save();
    $client2->save(); 

    PHP Code:
    DAO::setConnection($db);

    $doa = new ClientDAO();

    $client = new Client();

    $dao->save($client); 
    Personally, I prefer to make the connection a static property of the base class. I simply create it in the init and pass it.

    PHP Code:
    ActiveRecord::setConnection($db); 
    Simple - straight foward and don't need to worry about any more. If it needs to change I simple pass a new connection although that's rare.

    I'm not saying that solution would work for every situation but that's the method my library uses.

    Worry less about this and more about how to build the sql in a way where you avoid a new method for each and every little filter,group,select,limit and order change. You will also need to build a collection object for that data unless your just going to return a default array. However, if your joining tables together that have a one to many or many ot many relationship you'll end up with duplicated results. Passing the connection is a small decision in comparison to those.

  11. #11
    SitePoint Wizard
    Join Date
    Feb 2009
    Posts
    1,006
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yes, the database contains many many-to-many relations. Many complex queries. I believe it's normalized. (I have done my best), and it will count with 40 tables more or less.

    The propose is to create a database where all the Animal Shelters on my country could have a place to register their abandoned animals, do deal with adoptions, with donations, with associates, all from one place. This site will also contain some "smart parts", like: "if the end-user asks for a dog adoption, but the association don't reply in 2 days, the system will send a repeated e-mail to that association telling them to speed-up"... and other things like this.

    So things can became quite complex, and I want them to be.

    Considering this, and my painfull pass experience of doing it all over again, I'm searching from some advices about how can I properly separate, xml part + javascript part + php part + css part + mysql part, from this project. UPDATE: {I'm not talking physically, that I can manage, but logically speaking I find hard to project such separations}
    If, in the meanwhile, I can get some blocks of code to reuse on other (payable projects) great, if not, well, we will arrange that later on the way.

    I intend to add a little ajax if it's possible, so the separation of php part and javascript part it's not easy to imagine.


    I just want to start coding but having the scalability problems solved for the future, or, at least, and since I'm not a guru like you guys, to avoid the total chaos and probably ruin of the project on middle or long-term.
    Those are my concerns.


    What will be your best bets, remembering that I'm a newbie.
    I'm keeping repeating this, because the advices come in form of a factory and DI, and singletons and... WOW... Easy...




    I just need to be put on a right track...
    Any advice will be more then welcome,


    Regards,
    Márcio

  12. #12
    SitePoint Addict
    Join Date
    Nov 2005
    Location
    Germany
    Posts
    235
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by oikram View Post
    Yes, the database contains many many-to-many relations. Many complex queries. I believe it's normalized. (I have done my best), and it will count with 40 tables more or less.

    The propose is to create a database where all the Animal Shelters on my country could have a place to register their abandoned animals, do deal with adoptions, with donations, with associates, all from one place. This site will also contain some "smart parts", like: "if the end-user asks for a dog adoption, but the association don't reply in 2 days, the system will send a repeated e-mail to that association telling them to speed-up"... and other things like this.

    So things can became quite complex, and I want them to be.
    Not bad. This will be quite a challenge for a beginner. My very general advice here would be to just start with the most basic and simple features - one by one. Plan them, implement them, test them. Then do the next one. If a feature is hard to do (or you have duplicated code) look at what you have already and make changes so the new feature is easier to implement (refactoring). This way you will always (!) have something running (good for your moral!) and you don't get caught in endless upfront planning. I hate myself for throwing another buzzword at you, but have a look at unit testing, so you can (almost) always be sure you don't break existing features by adding new ones (which will be a major pain when the application becomes complex).

  13. #13
    SitePoint Wizard
    Join Date
    Feb 2009
    Posts
    1,006
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    @FrlB I really appreciate your advice. Danke!

    By "just start with the most basic and simple features", you mean by instance, let's keep on going with the "database connection" [one feature], next, the login page [next feature], next... ups... i need to add some ajax functionality on my checkifusernameexists function [next feature], when I do that previous ups, according to the "unit testing", I don't need to go back and remake the php code based on my "let's add this ajax functionality" feature?

    Will "unit test" prevent this from happening?

    I've also been told that:
    "Frameworks let you focus on the business. They settle a design foundation and do the repetitive work for you. And they organize the code and the layers very well. A lot of problems you will find are already solved on them, normally with good designs. There is no such things as "custom framework code". MVC frames only separate the layers and help you to put things on the right places, but what you write is as much "PHP code" as you would without one."

    It's tempting because I always wanted to avoid frameworks because I thought that they will do some code for me, and since I'm learning I don't want that.


    I'm more directed to follow the Unit Test lead then to follow the use a framework lead for now... but gash... that was tempting.


    Any thoughts are more then welcome...



    Regards,
    Márcio

  14. #14
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2006
    Location
    Augusta, Georgia, United States
    Posts
    4,194
    Mentioned
    17 Post(s)
    Tagged
    4 Thread(s)
    If you truly are interested in learning then your right to stay away from frameworks or libraries you didn't create in my opinion. I'm sure others have a different feeling, but you will learn more when you must do everything yourself that is a given.

  15. #15
    SitePoint Wizard
    Join Date
    Feb 2009
    Posts
    1,006
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by oddz View Post
    If you truly are interested in learning then your right to stay away from frameworks or libraries you didn't create in my opinion. I'm sure others have a different feeling, but you will learn more when you must do everything yourself that is a given.
    Uff. Thanks I just need a push on that direction. I don't want to just make. I want to make, knowing all about it.

    Do you agree about the Unit Test Approach ? It's a hard start. I will find any source I can get about:
    MVC for Web 2 - A total beginners, but exigents, Guide.


    Hmmm....


    Regards,
    Márcio

  16. #16
    Spirit Coder allspiritseve's Avatar
    Join Date
    Dec 2002
    Location
    Ann Arbor, MI (USA)
    Posts
    648
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by oddz View Post
    If you truly are interested in learning then your right to stay away from frameworks or libraries you didn't create in my opinion. I'm sure others have a different feeling, but you will learn more when you must do everything yourself that is a given.
    I'm starting to come around to the idea that the best way to learn isn't to do everything yourself... using a framework will, at bare minimum, help you understand what you don't like, so that you have a good understanding of what is required to make something that suits you better. Otherwise, in my limited experience, you end up repeating a lot of things that have already been done before, and probably haven't thought them through as much as the framework developers have. Just a thought.

    Do you agree about the Unit Test Approach ?
    I definitely do. Download SimpleTest (A unit testing framework) and go through their tutorials on their site.

  17. #17
    SitePoint Addict
    Join Date
    Nov 2005
    Location
    Germany
    Posts
    235
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by oikram View Post
    By "just start with the most basic and simple features", you mean by instance, let's keep on going with the "database connection" [one feature], next, the login page [next feature], next... ups... i need to add some ajax functionality on my checkifusernameexists function [next feature], when I do that previous ups, according to the "unit testing", I don't need to go back and remake the php code based on my "let's add this ajax functionality" feature?
    By basic feature I mean something more user centric like
    F1 "user may add an animal with a name to the database"
    F2 "user may edit animal properties name, age"
    F3 "user may view all animals"
    F4 "user must register before using the site"
    [...]
    Each of those has a small workflow, you may need a HTML form, a database connection, one or more "real life" (e.g. Animal, User) classes etc. You decide which features to do - but start simple. Don't think "big" from the start. There's a lot of challenges even in these simple features.

    If you have a good set of unit tests you will ensure that you don't break functionality needed by F1 by adding F2. You will always have to "remake" existing code to adjust the design and prevent code duplication. It's very likely to not get things right, but you should never be afraid to visit and improve "problem" areas. (I'm not sure I should have brought up the unit tests, but it's such a useful technique ...)

    Quote Originally Posted by oikram View Post
    I don't want to just make. I want to make, knowing all about it.
    Forget about that.

  18. #18
    SitePoint Wizard
    Join Date
    Feb 2009
    Posts
    1,006
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks a lot once again.

    I will not follow the framework smell, since I prefer understand how do things work, including the structure of things.

    I've read a little about Unit Tests, but without anyone that actually know what that his near me, I will be lost. It's quite a hard concept do catch alone. So I must leave it.

    I have only two (?) options left, and I please beg you, to give your experienced thoughts on this, knowing that those options are out, should I:

    Construct a very simple MVC structure and, on that structure, start coding each little part (little as frIB mentioned) ?

    Should I forget about MVC structure and just create my own structure and deal with it?


    I really appreciate some feedback on this,
    Regards,
    Márcio


    ps - I know almost nothing about MVC.
    ps2 - I can learn.

  19. #19
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2006
    Location
    Augusta, Georgia, United States
    Posts
    4,194
    Mentioned
    17 Post(s)
    Tagged
    4 Thread(s)
    I'm not sure there is much a thing as "simple" MVC pattern. Your either in or your out. Maybe you should try something out like cake just to get a feel then for MVC since you have little knowledge of it and wish to use the pattern.

  20. #20
    SitePoint Wizard
    Join Date
    Feb 2009
    Posts
    1,006
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    My bad, I forgot to share a link on a article I've been reading:
    http://nemetral.net/2008/08/13/a-gen...to-mvc-part-3/

    And there is obviously:
    http://nemetral.net/2008/07/31/a-gen...to-mvc-part-1/
    http://nemetral.net/2008/08/06/a-gen...to-mvc-part-2/

    I believe this is a very hard article to find, since it explores MVC on a very simple approach, leaving us room for improvements on long term.

    It's nothing obviously, compared with the MVC frameworks out there, but at least gives me a taste about structures on web applications development. On a future, if I feel like, I can take two ways: Improving my own MVC, or follow A NOW understandable to me, framework already on our disposal.
    The counter part on this is that, this IS the hard way. And I may get lost on the bushes when trying to implement simple tasks without a profond knowledge of the MVC pattern.

    Any of you believe that the MVC pattern can be "learned by doing"?




    Regards once again,
    Márcio


    ps- I promise I will end this discussion soon, and just make a decision, I only need some thoughts to put me on going...

  21. #21
    Spirit Coder allspiritseve's Avatar
    Join Date
    Dec 2002
    Location
    Ann Arbor, MI (USA)
    Posts
    648
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by oikram View Post
    I will not follow the framework smell, since I prefer understand how do things work, including the structure of things.
    PHP frameworks are open source, if you're using one you can look behind the scenes and see how things work, see the structure, and learn from the documentation and community. That seems to me to be a much quicker way to learn.

  22. #22
    SitePoint Addict
    Join Date
    Nov 2005
    Location
    Germany
    Posts
    235
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by oikram View Post
    I've read a little about Unit Tests, but without anyone that actually know what that his near me, I will be lost. It's quite a hard concept do catch alone. So I must leave it.
    Basic Unit Tests are quite simple to write, at the core they're really just fancy asserts (http://de3.php.net/assert).
    Skip it for now. Once your code base has grown and it starts to feel unsafe to make changes you can still add tests to your application.

    Quote Originally Posted by oikram View Post
    Any of you believe that the MVC pattern can be "learned by doing"?
    I guess you can. But be prepared that evolving an applications architecture is usually harder than normal code changes and additions.

  23. #23
    SitePoint Wizard
    Join Date
    Feb 2009
    Posts
    1,006
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    That's a good point frameworks are open source. Thanks for trouble me!!! Now I don't know anymore! AHHHhhh!!


    Well, now more seriously:

    Quote Originally Posted by FrlB View Post
    I guess you can. But be prepared that evolving an applications architecture is usually harder than normal code changes and additions.

    I will give it a try. If it gets to messy, then, back to trial / error aproach, with a butch of includes and repeated code. Since I'm learning, I really apreciate repeating tasks. Makes me learning to.



    Thanks for your comments.
    Any holy comments like: "Don't be stupid if you do as you think you will be killing yourself for nothing", are more then welcome.



    Regards,
    Márcio


    Ps- AH!!! that opensource argument!!! gash!!!!

  24. #24
    SitePoint Addict Mastodont's Avatar
    Join Date
    Mar 2007
    Location
    Czech Republic
    Posts
    375
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by allspiritseve View Post
    PHP frameworks are open source, if you're using one you can look behind the scenes and see how things work
    Most frameworks are in mature state and look behind is hard nut to crack. I agree with oikram, you need some simpler widget for studying.

  25. #25
    SitePoint Wizard
    Join Date
    Feb 2009
    Posts
    1,006
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Mastodont View Post
    Most frameworks are in mature state and look behind is hard nut to crack. I agree with oikram, you need some simpler widget for studying.
    Ufff... Ok. Please. No more framework temptation.
    Do you think it's easy for a newbie? See the punch lines like: you wanna develop fast and good, and shiny, come to us... we are... the framewor... NO!!!! NO!! I'm stronger! I don't believe you, you evil framework. Stay away from me!!



    Regards,
    Márcio


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
  •