SitePoint Sponsor

User Tag List

Results 1 to 10 of 10
  1. #1
    SitePoint Addict
    Join Date
    Nov 2006
    Posts
    205
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Help me with this OOP example

    I have a hard time thinking about OOP... And examples using the words Foo and Bar drive me crazy because I don't understand a thing... Visual type person haha

    SO I found an example that I'm sure would help clarify it for me...

    Alright say you have one formula that is
    "5 * [Variable]"
    and the other formula is
    "[Variable]"

    And one wanted to execute
    5 * [5 * [5 * [5 * [Variable]]]]

    Then this is where OOP would come in, right?
    If so, how would this look like in php?

  2. #2
    . shoooo... silver trophy logic_earth's Avatar
    Join Date
    Oct 2005
    Location
    CA
    Posts
    9,013
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    $v = new Number);
    $t = new Number);

    $v->times$t->power) );
    print 
    $v
    One way to look at it.

    Or could just do this:
    PHP Code:
    $v 7;
    print 
    pow5) * $v
    Logic without the fatal effects.
    All code snippets are licensed under WTFPL.


  3. #3
    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)
    This is absolutely nothing to do with OOP, whatsoever.

    This is the concept of OOP, but in Java - it's the same concept in PHP:
    http://sepwww.stanford.edu/sep/josman/oop/oop1.htm

    And this is how to implement it with PHP:
    http://www.thefullresource.com/viewi...ourse-:-:-PHP5
    Jake Arkinstall
    "Sometimes you don't need to reinvent the wheel;
    Sometimes its enough to make that wheel more rounded"-Molona

  4. #4
    SitePoint Wizard wonshikee's Avatar
    Join Date
    Jan 2007
    Posts
    1,223
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by arkinstall View Post
    This is absolutely nothing to do with OOP, whatsoever.

    This is the concept of OOP, but in Java - it's the same concept in PHP:
    http://sepwww.stanford.edu/sep/josman/oop/oop1.htm

    And this is how to implement it with PHP:
    http://www.thefullresource.com/viewi...ourse-:-:-PHP5
    I believe he is referring to how to handle that task in OOP methodology.

    To OP, what your asking isn't really going to clarify your understanding of OOP I don't think. Your asking for an example on an abstract object. The reason pretty much every example uses a real object is that it is easier to understand OOP on real objects, like a person, or a car and things those objects might do, ie: walk, jump, engine_start

    But to represent what your asking, you could use logic_earth's or like this
    PHP Code:
    $number = new Number(3);

    echo 
    $number->get_value(); # 3

    $number->multiply(5);

    echo 
    $number->get_value(); # 15

    $number->power(4);

    echo 
    $number->get_value(); # 625 
    So you treat the "number" as an object, and perform methods on it.

  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)
    You really wouldn't treat pure math any different in OOP than anywhere else. OOP is about managing complexity in large applications, by grouping code around concepts that are meaningful in the problem domain, rather than on the computers premises.

  6. #6
    SitePoint Addict
    Join Date
    Nov 2006
    Posts
    205
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for all the tips... This seems to be where I hit the wall
    I guess I'm going to need to find a good book

    because I'm getting the impression that functions and OOP are completly different things

  7. #7
    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)
    There isn't really need for a book to get you started.

    http://sepwww.stanford.edu/sep/josman/oop/oop1.htm

    This tutorial is written with Java in mind but it really gives the reader a good idea of what OOP is (and no matter what language, OOP concepts are the same).
    Jake Arkinstall
    "Sometimes you don't need to reinvent the wheel;
    Sometimes its enough to make that wheel more rounded"-Molona

  8. #8
    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 prince_mallow View Post
    because I'm getting the impression that functions and OOP are completly different things
    That's correct. Objects are an addition to the simpler procedural programming model. You still have functions, but they are grouped into small clusters, called classes. These classes can be instantiated as objects.

    One way to think about objects, is that they are like mini-programs. Instead of having one big program, you divide it into a number of smaller, autonomous objects, that can interact with each other. Because they are much smaller, they also tend to be less complicated. In this way, writing object oriented code, can reduce the complexity of a large application.

  9. #9
    SitePoint Guru
    Join Date
    Jul 2005
    Location
    Orlando
    Posts
    634
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by kyberfabrikken View Post
    That's correct. Objects are an addition to the simpler procedural programming model. You still have functions, but they are grouped into small clusters, called classes. These classes can be instantiated as objects.
    Right, and functions that are grouped into classes are called "methods". And variables that are grouped into classes are called "properties".

    So, in OOP a method is just a function that's specific to a class. It's a way to organize and group things together so they make more sense.

    In addition, you don't need to worry as much about duplicate functions. Since each object has its own namespace for method names, there's less collision.

  10. #10
    SitePoint Addict
    Join Date
    Aug 2007
    Location
    St. Louis, MO.
    Posts
    206
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I recommend this book. So far its been the best book I have found that is geared towards those that know php but have never delt with objects and patterns. It goes from 0-60 pretty quickly but I have had a few pretty good "ah ha!" moments with it.

    I found this article very helpful. When I got to the bottom I realized it was written by the same guy.

    I know enough now to realize how little I actually know. I'm learning that means I'm making progress


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
  •