SitePoint Sponsor

User Tag List

Results 1 to 20 of 20
  1. #1
    SitePoint Enthusiast eneza's Avatar
    Join Date
    Feb 2009
    Location
    Philippines
    Posts
    84
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Lightbulb LOGIC first or OOP first?

    I am new in OOP I have few systems written in PROCEDURAL but as I advance, and the system become complicated, the page become heavier and heavier so I need to CLASS it.

    I am getting a hard time to go around with classes, is it my logic or OOP is just HARD?

  2. #2
    SitePoint Addict hiddenpearls's Avatar
    Join Date
    Dec 2007
    Location
    Pakistan
    Posts
    262
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Nothing is hard actually , u need to manage the things just with OOP.
    OOP is easy to manage once u will do it first time .
    First time managing takes time . Constructors ,inheritance , Polymorphism etc

  3. #3
    . shoooo... silver trophy logic_earth's Avatar
    Join Date
    Oct 2005
    Location
    CA
    Posts
    9,013
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    Object Oriented Programing is a whole different mentality then it is with procedural. I've yet to meet anyone that understood OO right away when coming from a procedural experience. It will take time for it to set in.
    Logic without the fatal effects.
    All code snippets are licensed under WTFPL.


  4. #4
    SitePoint Enthusiast
    Join Date
    Nov 2007
    Posts
    63
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I am being fan of OOP. I promise you that OOP is much better than any structured language. I am not very sure how coding OOP is harder than procedure. It may seem OOP is hard and burden some but resusing property of OOP makes life simpler.

  5. #5
    SitePoint Enthusiast
    Join Date
    Nov 2007
    Posts
    63
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by logic_earth View Post
    Object Oriented Programing is a whole different mentality then it is with procedural. I've yet to meet anyone that understood OO right away when coming from a procedural experience. It will take time for it to set in.
    I would agree. It took me some time to get the concept of class and object. I know ppl migrating from structured languages (like C) to Java or C++ find OOP easier.

  6. #6
    SitePoint Guru
    Join Date
    Jan 2005
    Location
    heaven
    Posts
    953
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I actually went backwards. I started with OOP then went to procedural then back to OOP. >.>
    Creativity knows no other restraint than the
    confines of a small mind.
    - Me
    Geekly Humor
    Oh baby! Check out the design patterns on that framework!

  7. #7
    SitePoint Enthusiast
    Join Date
    Nov 2007
    Posts
    63
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by imaginethis View Post
    I actually went backwards. I started with OOP then went to procedural then back to OOP. >.>
    mE 2.Learned java first then C

  8. #8
    SitePoint Addict hiddenpearls's Avatar
    Join Date
    Dec 2007
    Location
    Pakistan
    Posts
    262
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by uniqueumang View Post
    mE 2.Learned java first then C
    with time, u can work with many languages .
    important are the concepts !

  9. #9
    SitePoint Enthusiast
    Join Date
    Nov 2007
    Posts
    63
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by hiddenpearls View Post
    with time, u can work with many languages .
    important are the concepts !
    I don't see any difference in any language. They all same to me(except Ruby).A weird intrsting language.

  10. #10
    Floridiot joebert's Avatar
    Join Date
    Mar 2004
    Location
    Kenneth City, FL
    Posts
    823
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    As long as you remember the words "instances of" you'll catch on in no time.

  11. #11
    SitePoint Enthusiast eneza's Avatar
    Join Date
    Feb 2009
    Location
    Philippines
    Posts
    84
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Thumbs up

    Quote Originally Posted by joebert View Post
    As long as you remember the words "instances of" you'll catch on in no time.
    I will note that... Thanks for the TIP!

  12. #12
    SitePoint Guru
    Join Date
    Jun 2006
    Posts
    638
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by eneza View Post
    I am getting a hard time to go around with classes, is it my logic or OOP is just HARD?
    logic... your not thinking in the OOP way.
    before you write any code, write the comments of what you need to do.

    If you do this for all your project, you will start to see patterns.
    You will see duplicated code, and start moving it in "classes" (your not OOP yet).

    After you do that, you will see that some code relates to other code.
    That you have parts of a class that relates to other parts of other classes.

    Then you will start refactoring your classes, so one class deals with one thing only, and that it has small functions that deal with only one thing within that class and so on.

    And before you know it, all your logic is within a few classes, and your "heavy page" will look really simple, and you will have a project codded the OOP way.

  13. #13
    Non-Member adstiger's Avatar
    Join Date
    Nov 2008
    Location
    Canada
    Posts
    348
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    concepts are most important i.e. logic comes first then will come any other thing like oops.

  14. #14
    SitePoint Wizard
    Join Date
    Mar 2008
    Posts
    1,149
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It seems that you (eneza), are looking at classes as a way to reuse code. Functions were made to let you reuse code, not classes. Classes are for situations where you are "working on" one particular instance of something.

    Let me show you an example to help you get the idea of classes. Say you have a file system and you want to move files around. You have the following functions: (Note: not written in PHP or any particular language)
    Code:
    void move(string path, string destination);
    void delete(string path);
    string read(string path);
    void write(string path, string content);
    Let's say you want to work with a file at "/home/you/info.txt". You can call the respective functions on that file:
    Code:
    echo read("/home/you/info.txt");
    move("/home/you/info.txt", "/home/you/info2.txt");
    delete("/home/you/info2.txt");
    Notice how you are working on the same file and yet you have to to keep telling each function what file you want to work on. Wouldn't it be great if the functions knew what file it was to work on? That's where classes come into the picture. Now, you can define the following class:
    Code:
    class File {
        File(string path); // This is the constructor
        void move(string destination);
        void delete();
        string read();
        void write(string content);
    }
    Every time you call one of those methods, the method knows what you want to work on because the method has access to $this (in PHP). $this is actually passed implicitly when you call your method.

    Now the code can be cleaner:
    Code:
    File f = new File("/home/you/info.txt");
    echo f.read();
    f.move("/home/you/info2.txt");
    f.delete();
    Note that when you move the file, the path would be automatically updated in the instance of the object. You don't have to bother keeping track of it yourself. (But you do need to write the code to keep track of that in the move() method).

    Using classes to re-use code is wrong, like in this example:
    Code:
    class RandomFunctions {
        int randomInt();
        int currentTimestamp();
        string removeBadWords(string text);
    }
    Those methods are not "working on" any particular thing.

    ---

    EDIT: To better explain $this and how methods know what to work on:

    This might all make better sense if you try classes in Python. $this is not passed as a parameter in your methods implicitly. You have to explicitly declare it:
    Code python:
    class File:
        def __init__(self, path):
            self.path = path
        def move(self, destination):
            # write some code to move the file
            self.path = destination
        # etc...
    See how it's just like procedural code now? However, in Python, while you have to declare it explicitly when defining the method, Python will automatically add the parameter for you when you call the object's methods. Otherwise, it would be very pointless.
    Code python:
    f = File("/home/you/info.txt")
    f.move("/home/you/info2.txt");
    f.delete()
    # etc...
    This is what it really is doing:
    Code python:
    f = File("/home/you/info.txt")
    File.move(f, "/home/you/info2.txt");
    File.delete(f)
    # etc...
    Just like procedural!

    ---

    Note: In purely OOP languages, you have to use classes to organize functions. In PHP, you get the luxury of using them as real and true objects. Organizing functions in PHP right now is hard though because you can't use namespaces to categorize your random functions yet. However, PHP 5.3 (if it ever comes out) should change that.

  15. #15
    SitePoint Enthusiast
    Join Date
    Nov 2007
    Posts
    63
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by sk89q View Post
    I don't think that you should approach OOP as something where you can reuse code. Functions were made to let you reuse code, not classes. Don't even consider that as a use of class. You want to use a class when you have many functions that need to work on the same instance of something. If you have a PDF file that you "work on" using different functions, then it belongs in a class.
    You are partially correct . I think we optimize the use of the code with classes. My life sucked when I use to code in C. You may have realized that if you have a good generalized class, then you can simply import it and start using it . Unfortunately you can't do so in functions.you have to go thru the pain of configuring the function. You can group classes(extend classes), build interface , use of abstract classes are fine example of reusing the code.

  16. #16
    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)
    Whilst I did get the basics, I really didn't get the hang of pure OOP until I tried out C# and Java. I recommend it.

    Though in PHP, techniques are slightly different because of its request-based nature.
    Jake Arkinstall
    "Sometimes you don't need to reinvent the wheel;
    Sometimes its enough to make that wheel more rounded"-Molona

  17. #17
    SitePoint Enthusiast
    Join Date
    Nov 2007
    Posts
    63
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If you have some basic knowledge (about any programming language excluding HTML AND CSS) I would recommend php anthology 101 book. If you never done programming at all I would recommend php book by kevin yank.Please note that Kevin book is very basic, and doesn't even touch OO topics.

  18. #18
    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)
    Avoid any book older than 2-3 years old if you are going to buy any book on PHP.

    PHP5.2 was a big benchmark in the PHP release history, and was released late 2006.

    Any book older isn't going to be that reliable, as things change massively with the introduction of new functions etc. Books are written using the current standards - and they change over time. Heck, Kevin Yank's books used to promote Register Globals - not because they were bad books but because Register Globals was standard back then.
    Jake Arkinstall
    "Sometimes you don't need to reinvent the wheel;
    Sometimes its enough to make that wheel more rounded"-Molona

  19. #19
    SitePoint Enthusiast eneza's Avatar
    Join Date
    Feb 2009
    Location
    Philippines
    Posts
    84
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Thumbs up

    Quote Originally Posted by Vali View Post
    logic... your not thinking in the OOP way.
    before you write any code, write the comments of what you need to do.

    If you do this for all your project, you will start to see patterns.
    You will see duplicated code, and start moving it in "classes" (your not OOP yet).

    After you do that, you will see that some code relates to other code.
    That you have parts of a class that relates to other parts of other classes.

    Then you will start refactoring your classes, so one class deals with one thing only, and that it has small functions that deal with only one thing within that class and so on.

    And before you know it, all your logic is within a few classes, and your "heavy page" will look really simple, and you will have a project codded the OOP way.
    Very Well said!!!!! Thanks Man!!! So using classes is not enough to call your page OOP-CODDED PAGE?! Interesting.... Thanks Man!

  20. #20
    PHP/Rails Developer Czaries's Avatar
    Join Date
    May 2004
    Location
    Central USA
    Posts
    806
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by eneza View Post
    Very Well said!!!!! Thanks Man!!! So using classes is not enough to call your page OOP-CODDED PAGE?! Interesting.... Thanks Man!
    Vali made a great post very accurately following the reality of learning OOP. Classes are just a method of organizing code. They are just a blueprint for creating an object. Making classes is easy. True OOP is hard, and it seems to be exceedingly rare to find it in PHP land.

    True OOP isn't really about code re-use, though that's what a lot of people say. Slapping your code into a procedural function or a utility class is re-use. OOP is about encapsulation, abstraction, and isolating the responsibilities of your code down to manageable units, then finding a way to make those different code units work together in a way that does not explicitly tie them to each other. Freely re-using code, plugging in different parts, and completely changing whole layers of functionality with a few lines of code is just a side-effect of good OOP.


Tags for this Thread

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
  •