SitePoint Sponsor

User Tag List

Page 1 of 3 123 LastLast
Results 1 to 25 of 51

Thread: Do you do OOP?

  1. #1
    SitePoint Addict
    Join Date
    Jul 2009
    Posts
    221
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Do you do OOP?

    Do you?

    If you do, what are the steps you take from design to actual production?

    I'm a lonely programmer and I keep repeating codes. Haven't realized the power of OOP yet cause I don't know how to start. Don't know what are the classes I need. Don't know how to design classes that could be used in other projects.

  2. #2
    SitePoint Zealot Kayarc's Avatar
    Join Date
    Sep 2009
    Posts
    127
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP's manual has a bit on OOP:
    http://www.php.net/oop

    If you do not insist on writing code from scratch, use an OOP framework like CodeIgniter, Symfony, or CakePHP
    Phoenix Arizona Web Design | info *at* kayarc.com | 602.633.2676

  3. #3
    SitePoint Guru
    Join Date
    Mar 2006
    Posts
    701
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    My experience says too that OOP is very good.
    I like the style of book .
    If you want to use a framework I thing that it is obvious to practice first and then to use it at a production evironment.
    Look also at yiiframework.com

  4. #4
    SitePoint Zealot Kayarc's Avatar
    Join Date
    Sep 2009
    Posts
    127
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by dimis283 View Post
    Look also at yiiframework.com
    What are people's thoughts on Yii Framework?
    Phoenix Arizona Web Design | info *at* kayarc.com | 602.633.2676

  5. #5
    SitePoint Enthusiast
    Join Date
    Dec 2003
    Location
    norway
    Posts
    61
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by resting View Post
    Do you?

    If you do, what are the steps you take from design to actual production?

    I'm a lonely programmer and I keep repeating codes. Haven't realized the power of OOP yet cause I don't know how to start. Don't know what are the classes I need. Don't know how to design classes that could be used in other projects.

    To design classes that could be used in other projects, you should, for example, not have dependencies inside your class, instead add those dependencies after object creation
    $model = New Model();
    $model->SetDataBaseConnectionString("...");

  6. #6
    SitePoint Guru
    Join Date
    Mar 2006
    Posts
    701
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Kayarc View Post
    What are people's thoughts on Yii Framework?
    I use Yii and my opinion is that is a good framework.
    It use OOP and MVC patern, you may as example extend a base class of the framework or use and extend other classes or just create your own.
    You may look here

  7. #7
    SitePoint Addict
    Join Date
    Jul 2009
    Posts
    221
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thats a very nice book you recommended dimis283
    Yii is something new to me. how does it compare to those popular ones like zend, cakephp, codeigniter?

    i guess to design a software, we should start with a UML diagram? any recommended open source solution for that? like i'm using mysql workbench for doing ERD diagrams.

  8. #8
    SitePoint Guru
    Join Date
    Mar 2006
    Posts
    701
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yii is relative new but has its base at prado, I used CI and I thing Yii is a better choice.
    As I notice have many simiralities with cake as they are from Ruby on Rails as they saying.
    Netbeans.org has support of uml and php.

  9. #9
    PHP/Rails Developer Czaries's Avatar
    Join Date
    May 2004
    Location
    Central USA
    Posts
    806
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I tend to model my data first. Recently, I just start with an ORM immediately. Data access and updating is something that happens all throughout your app, so its important to centralize all the functionality in a single place right off the bat. Otherwise, you end up with queries in a bunch of places they shouldn't be and code responsibilities can end up mixed pretty badly. OOP itself doesn't solve these design issues or prevent you from doing things you shouldn't.

  10. #10
    SitePoint Wizard
    Join Date
    Apr 2007
    Posts
    1,401
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    These day and age, developers don't do OO design as much as before. It's not like we're building from scratch. I'd say savy developers are more like "Open Source Architect". They would add the ingredients needed to make the developers to code faster! So, most of the time you just need build few classes that extend their framework. I hardly call Data model as OO design but if you do then this is where majority of OO design would fit in.

    If you ever do java then these 3 are the hot tech to learn

    JSF + Spring + Hibernate

    By learning these, you'll automatically understand OOP.

  11. #11
    John 8:24 JREAM's Avatar
    Join Date
    Sep 2007
    Location
    Florida
    Posts
    1,508
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ive been struggling to understand OOP for a while, and after messing around so long I'm slowly beginning to catch on. Be patient with learning it, it's kind of tricky.

    I think the first thing you should try doing is making a MySQL Class, so that inserting and selecting is really easy, then you use 1 object of the mysql class through your entire aplplication.

  12. #12
    SitePoint Evangelist
    Join Date
    Aug 2005
    Location
    Winnipeg
    Posts
    498
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by resting View Post
    Do you?

    If you do, what are the steps you take from design to actual production?

    I'm a lonely programmer and I keep repeating codes. Haven't realized the power of OOP yet cause I don't know how to start. Don't know what are the classes I need. Don't know how to design classes that could be used in other projects.
    OOP alone won't make your code better, you can write non-cohesive and highly coupled code in OO languages, just as you can break OO best practices while using OOP.

    Brush up on OOD principles: http://ootips.org/ood-principles.html

    Decomposing an object isn't easy, nor can be it be easily explained, each problem is different. What good OOP will facilitate is easier refactoring, so further decomposition of an object API becomes much easier.

    Just remember DRY KISS...keep everything extremely simple...no method greater than 25 SLOC lines or whatever your comfortable with...
    The only constant in software is change itself

  13. #13
    Theoretical Physics Student bronze trophy Jake Arkinstall's Avatar
    Join Date
    May 2006
    Location
    Lancaster University, UK
    Posts
    7,062
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by PCSpectra View Post
    OOP alone won't make your code better
    Maybe so, but I'd definitely say that the learning process does. It adds another dimension to the way you think about programming. If you write a program procedurally, then learn OOP to a decent level, write the program in OOP then write it again procedurally, it's safe to say that all 3 versions will be very different.

    Generally the 3rd (Procedural, after learning OOP concepts) will be neater and more structured than the 1st (Procedural, before learning OOP concepts), and the 2nd (OOP) and 3rd (Procedural Post-OOP) could be similar in some ways.
    Jake Arkinstall
    "Sometimes you don't need to reinvent the wheel;
    Sometimes its enough to make that wheel more rounded"-Molona

  14. #14
    SitePoint Zealot Kayarc's Avatar
    Join Date
    Sep 2009
    Posts
    127
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    OOP seems to be a different way of organizing and orchestrating your applications flow. When I first started, I wrote functions for everything and they ended up everywhere. At that point I hated it. I've been sticking with it and, although I'm still noob I suppose, really like it. For me it is far superior to procedural in many circumstances. Of course, there are times procedural makes more sense than OOP but generally only when you are writing something on a smaller scale.
    Phoenix Arizona Web Design | info *at* kayarc.com | 602.633.2676

  15. #15
    SitePoint Member
    Join Date
    Sep 2009
    Location
    Midland, MI, USA
    Posts
    24
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    CodeIgniter is your friend, it forced me to use OOP and I haven't looked back. I no longer re-write code as all my classes are very modular.

    CI uses the MVC approach for development, very useful for finding a good starting point with your application development.
    -- Thomas Hunter, BS, ZCE

    NeoInvoice SITEPOINT2010 - Free 6 months for Sitepoint members!

  16. #16
    SitePoint Zealot
    Join Date
    Nov 2008
    Location
    UK
    Posts
    163
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I am just getting to grips with oop. I am rewriting an ecommerce site to use it, and there are some cases when i don't know whether to use a class or just keep calling a function/include.

    For example, inserting the google analytics code. To insert a chunk of code like that (something that will never change, and will never be inherited from) is a class the best
    option, or an include, or a function?. Which takes the least processing?

    Another example - i have a function, checkSession which returns the value of a session var or false. Presumably a quick lookup like that should remain a stand-alone function?

  17. #17
    SitePoint Enthusiast
    Join Date
    Dec 2003
    Location
    norway
    Posts
    61
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by treacle0996 View Post
    For example, inserting the google analytics code. To insert a chunk of code like that (something that will never change, and will never be inherited from) is a class the best
    option, or an include, or a function?. Which takes the least processing?

    Another example - i have a function, checkSession which returns the value of a session var or false. Presumably a quick lookup like that should remain a stand-alone function?
    Something like google analytics code would maybe be not be a candidate for a class, however I think the issue is also how you structure your site, do you want to mix php includes with classes for example? Therefore it could be wise to make a statistics class just to keep thing organised.

    "checkSession" function seems to belong to a specific class, maybe a session or user class depending on your class structure...so avoid a stand alone function for this

  18. #18
    Founder of Primal Skill Ltd. feketegy's Avatar
    Join Date
    Aug 2006
    Posts
    482
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It's absolutely BAD choice using a PHP framework before knowing even basic OOP concepts, it's will mess you up...

    First, learn the OOP principles, then try to make simple applications using OOP, only after this try a PHP framework...

    Try reading a book on OOP, because it will give you a more systematic learning, than on blogs.

    I recommend these books:
    Object-Oriented Programming with PHP5
    PHP 5 Objects, Patterns, and Practice
    The Object-Oriented Thought Process

  19. #19
    SitePoint Zealot
    Join Date
    Nov 2008
    Location
    UK
    Posts
    163
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    danman, re the checksession example. I use that everywhere.
    eg -
    if user is logged, don't show login tab, show account instead.
    if basket is not empty, show checkout option
    check if user tried to access the checkout page without logging in first (so it knows to redirect after login)
    for the id of the basket to add lines to

    do you suggest i add it as a function to all sorts of classes?

  20. #20
    SitePoint Enthusiast
    Join Date
    Dec 2003
    Location
    norway
    Posts
    61
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by treacle0996 View Post
    danman, re the checksession example. I use that everywhere.
    eg -
    if user is logged, don't show login tab, show account instead.
    if basket is not empty, show checkout option
    check if user tried to access the checkout page without logging in first (so it knows to redirect after login)
    for the id of the basket to add lines to

    do you suggest i add it as a function to all sorts of classes?
    No not really..
    A good approach could be to put the session handling in a class to be able to change that implementation if needed later on

    Code PHP:
    class Session
    {
    public function __construct()
    {
    session_start();
    }
     
    public function get($name)
    {
    if (isset($_SESSION[$name]))
    {
    return $_SESSION[$name];
    }
    else
    {
    return false;
    }
    }
    ....
    }

    And then the user class or basket class or something has specific methods
    for example a "IsLoggedIn" method that uses the Session class...

    Code PHP:
    if($user->IsLoggedIn())// do something

  21. #21
    SitePoint Guru risoknop's Avatar
    Join Date
    Feb 2008
    Location
    end($world)
    Posts
    834
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Take a look at Zend Framework, I've been using it for months now and it has increased my productivity dramatically

  22. #22
    simple tester McGruff's Avatar
    Join Date
    Sep 2003
    Location
    Glasgow
    Posts
    1,690
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by resting View Post
    Don't know what are the classes I need. Don't know how to design classes that could be used in other projects.
    It's all about throwing balls.

    When you throw a ball a lot of things happen. Nerves fire. Muscles flex: brachialis anticus, deltoid, biceps, pectoralis major, coracobrachialis - all the many muscles of the arm. And not just the arm. Millions of years of evolution have hard-wired Newton's laws of motion into our command and control systems. When you throw a ball, you don't fall over with that every-action-has-an-opposite-reaction thing. Almost every muscle in your body has a part to play in throwing the ball, maintaining balance and just generally looking well-buffed in case anyone is watching.

    There's a *lot* going on, when you stop to think about it, too much for us to actually comprehend in real-time. We'd be overwhelmed with data. Luckily we don't have to. We just think "throw the ball" and moments later, there it is, flying through the air.

    That's OOP.

    A class presents a simple interface to all the fiddly details. We don't really care what they are (at least not at the point of use). We just want to throw the ball, as quickly and simply as possible. We've got lots of other things to do as well and there's a limit to what we can hold in our heads at one time. A nice simple interface to a complex set of actions helps us to understand what's going on.

    What should be encapsulated? Good classes tend to do just one thing. They round up a bunch of related data and behaviour in one place rather than spreading it around all over the app. That is the key skill: figuring out how to cut the logic cake to make good, coherent components. Sometimes there isn't only one answer just a series of choices and consequences.

  23. #23
    SitePoint Member
    Join Date
    Sep 2009
    Posts
    6
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    For me i use OOP framework like CodeIgniter, Symfony, or CakePHP since i do not write code from scratch....its more easy and effective...

  24. #24
    SitePoint Zealot
    Join Date
    Nov 2008
    Location
    UK
    Posts
    163
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for that danman.

    So, you are suggesting that the isLoggedIn method would create a new Session instance ?
    Or would I create an instance at the start of each page and pass that through to User? - and any other classes that needed it

    This is the part i'm finding hard to get my head around - when to create new instances and when to pass things through. It seems wrong to create an instance every time i just need to get a single value. Isn't there a big overhead to that?

  25. #25
    SitePoint Enthusiast
    Join Date
    Dec 2003
    Location
    norway
    Posts
    61
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by treacle0996 View Post
    Thanks for that danman.

    So, you are suggesting that the isLoggedIn method would create a new Session instance ?
    Or would I create an instance at the start of each page and pass that through to User? - and any other classes that needed it

    This is the part i'm finding hard to get my head around - when to create new instances and when to pass things through. It seems wrong to create an instance every time i just need to get a single value. Isn't there a big overhead to that?
    Yes "isLoggedIn" method could create a new Session instance. I dont think it will cause any problems.
    Do not create an instance at the start of each page, it seems unnesesarry since you dont know if it will be needed..in my opinion ..

    Consider a singleton approach on the session class since session_start(); for example should not be called more then one time ..

    I dont think you have to worry about overhead for this...
    Anyway the question about creating an instance or not or call static functions is debated back and forth and i think it it hard to answer in generel terms..


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
  •