SitePoint Sponsor

User Tag List

Results 1 to 11 of 11
  1. #1
    SitePoint Zealot Krieger's Avatar
    Join Date
    Dec 2003
    Location
    Nevada - USA
    Posts
    130
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    OOP newcomer question

    Hello.

    I have zero prior experience in OOP, but have been reading extensively. I have recently undertaken to write my php in OOP as opposed to straight procedural (I write programs for fun, not professionally obviously). I'm writing in php 5.0.3.

    I have a question for the more experienced here:

    Take the following example:

    I instantiate an object, and call a method of said object passing in a property (attribute) thus:

    PHP Code:
    //includes etc up here
    $my_var 'value';
    $my_obj = new Some_Class();
    $new_val $my_obj->someFunction($my_var); 
    My question is: Is it considered proper practice to do this in someFunction:

    PHP Code:
    public function someFunction($my_var)
    {
        
    $this->my_var $my_var;

        
    // stuff happens here......

    or is it proper to use this construct:

    PHP Code:
    public function someFunction($my_var)
    {
        
    // stuff happens here using only $my_var

    I typically use the $this-> construct when I will need access to the property elsewhere in the class, otherwise I use the second example.

    Does anyone have any advice/guidance to offer regarding this? Thank you.
    Last edited by Krieger; Feb 10, 2005 at 13:05.
    Verwüstung rufen und des Krieges Hund' entfesseln!

    My Site
    My first php/MySQL project -->Thanks Sitepoint members

  2. #2
    SitePoint Addict
    Join Date
    Nov 2001
    Posts
    213
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You only need to use $this when referring to a variable outside local scope, or whatever the right terminology is (hey, I code, not write manuals!). Here's a modified example from the PHP manual:

    PHP Code:
     class MyClass
     
    {
         public 
    $public 'Public';
         protected 
    $protected 'Protected';
         private 
    $private 'Private';
     
         function 
    printHello($myvar)
         {
             echo 
    $this->public;
             echo 
    $this->protected;
             echo 
    $this->private;
            return 
    $myvar;
         }
     } 
    See how they use $this only when setting properties (vars) outside the function? I added $myvar to show that the function can use it without using $this in front.

    Also, your object instantiation code doesn't need to use $this either:

    PHP Code:
     $my_var 'value'
    $my_obj = new Some_Class(); 
    $new_val $my_obj->someFunction($my_var); 
    Since $my_obj is a normal variable of type object. $this is used specifically inside classes to refer to properties outside of a method. I can't think of any other places to use it.

  3. #3
    SitePoint Zealot Krieger's Avatar
    Join Date
    Dec 2003
    Location
    Nevada - USA
    Posts
    130
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for the response.

    It seems as though I am on the right track.

    I just want to adhere to generally accepted practices regarding this. I'm retentive, and should I find out I did it 'wrong' I would rewrite all of it. I would MUCH rather do it a better way the first time through (not counting my 17 bug rewrite fixes) and save myself the time.
    Verwüstung rufen und des Krieges Hund' entfesseln!

    My Site
    My first php/MySQL project -->Thanks Sitepoint members

  4. #4
    ********* Victim lastcraft's Avatar
    Join Date
    Apr 2003
    Location
    London
    Posts
    2,423
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Hi...

    I would pass stuff in the constructor if possible as you want to initialise to a valid state. A partly initialised object will be a major headache. There are some exceptions to this rule...
    1) The value has a setter anyway because it isn't constant. In that case a constructor option might be needless duplication.
    2) The parameter is one of many optional ones. You still have to ensure that if the value is missing the object will still work.
    3) There are too many constructor parameters right now. Probably you have deeper design problems if this is the case.

    Get a copy of "Refactoring" by Martin Fowler. Lot's of good coding practices in that book.

    yours, Marcus
    Marcus Baker
    Testing: SimpleTest, Cgreen, Fakemail
    Other: Phemto dependency injector
    Books: PHP in Action, 97 things

  5. #5
    SitePoint Zealot Krieger's Avatar
    Join Date
    Dec 2003
    Location
    Nevada - USA
    Posts
    130
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Wow.

    My first post here and I get an answer from Mr. Baker.

    Thanks for the reading tip Marcus. I'll definitely check that out.

    Cheers.
    Verwüstung rufen und des Krieges Hund' entfesseln!

    My Site
    My first php/MySQL project -->Thanks Sitepoint members

  6. #6
    SitePoint Evangelist
    Join Date
    May 2004
    Location
    Germany
    Posts
    550
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
     public function someFunction($my_var)
    {
        
    $this->my_var $my_var;

        
    // stuff happens here......

    Is $my_var used in another method of the class or only in someFunction() ?
    If the latter i'd go with using $my_var. if someFunction() calls other methods internally that also need $my_var, i'd use $this->my_var.

    Hard to say with such an abstract example

    Off Topic:


    Urschrei verwüstung und freigeben die hunde des krieges!
    This sounds strange/wrong
    where did you get that?

  7. #7
    SitePoint Wizard
    Join Date
    Aug 2004
    Location
    California
    Posts
    1,672
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    One of the benefits of passing parameters to the constructor is that you can to this:
    PHP Code:
    $someObj->add(new myClass('param1'$param2)); 
    Rather than:
    PHP Code:
    $myObj = new myClass();
    $myObj->setVar1('param1');
    $myObj->setVar2($param2);
    $someObj->add($myObj); 
    It is part a style issue, but it allows objects to be treated like a var, which is handy in many cases. Objects have a var/code duality and it opens up more implementation possiblities to have the option of how you use them.

  8. #8
    SitePoint Zealot Krieger's Avatar
    Join Date
    Dec 2003
    Location
    Nevada - USA
    Posts
    130
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Daimaju
    PHP Code:
     public function someFunction($my_var)
    {
        
    $this->my_var $my_var;

        
    // stuff happens here......

    Is $my_var used in another method of the class or only in someFunction() ?
    If the latter i'd go with using $my_var. if someFunction() calls other methods internally that also need $my_var, i'd use $this->my_var.

    Hard to say with such an abstract example

    Off Topic:



    This sounds strange/wrong
    where did you get that?
    On topic: Your question is exactly what I meant in my example. It seems as though I am on track.

    Off topic: It's 'Cry havoc and let slip the dogs of war' from Shakespeare's Julius Caesar.

    I know that the translation is poor. The phrase does not translate well in German. My Uncle-in-law gave me a rough translation (he's Austrian), but told me it 'just won't translate very well'. He said that urschrei is 'primal scream' but the context is tricky.


    Can you offer a better translation?
    Verwüstung rufen und des Krieges Hund' entfesseln!

    My Site
    My first php/MySQL project -->Thanks Sitepoint members

  9. #9
    SitePoint Zealot Krieger's Avatar
    Join Date
    Dec 2003
    Location
    Nevada - USA
    Posts
    130
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by arborint
    One of the benefits of passing parameters to the constructor is that you can to this:
    PHP Code:
    $someObj->add(new myClass('param1'$param2)); 
    Rather than:
    PHP Code:
    $myObj = new myClass();
    $myObj->setVar1('param1');
    $myObj->setVar2($param2);
    $someObj->add($myObj); 
    It is part a style issue, but it allows objects to be treated like a var, which is handy in many cases. Objects have a var/code duality and it opens up more implementation possiblities to have the option of how you use them.
    I often do just that in the constructor. I suppose, as Daimaju pointed out, my example was not exemplary.

    I have, as you stated, found that to be useful. I'm not a CS student, so my progress relative to the OOP learning curve will be slow. Tips such as all those above are a big help in filling in some gaps.

    Thanks to all who responded.
    Verwüstung rufen und des Krieges Hund' entfesseln!

    My Site
    My first php/MySQL project -->Thanks Sitepoint members

  10. #10
    SitePoint Evangelist
    Join Date
    May 2004
    Location
    Germany
    Posts
    550
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    to learn more about oop you can also take a look at www.phppatterns.com , http://www.sitepoint.com/article/object-oriented-php or the sp-forums

    Off Topic:


    Mord rufen und des Krieges Hund' entfesseln. (from wikiquote)
    But you can replace Mord (murder/ homicide) with Verwüstung/Chaos (havoc)

    This is still strange german, but i guess Shakespeare sounds strange in every language

  11. #11
    SitePoint Zealot Krieger's Avatar
    Join Date
    Dec 2003
    Location
    Nevada - USA
    Posts
    130
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Deutsch wiki, very slick.

    Danke for both the OOP tip, and a cleaner quote.
    Verwüstung rufen und des Krieges Hund' entfesseln!

    My Site
    My first php/MySQL project -->Thanks Sitepoint members


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
  •