SitePoint Sponsor

User Tag List

Results 1 to 19 of 19
  1. #1
    SitePoint Zealot
    Join Date
    Aug 2003
    Location
    Singapore
    Posts
    105
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Passing by reference

    Hi

    I want to know when I should pass by reference (in oop), can anyone provide an answer for that? Also, everything in the constructor of a class gets executed like in procedural programming am I right (unlike the other functions which only execute when they are called?)

  2. #2
    Non-Member
    Join Date
    Jan 2003
    Posts
    5,748
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Umm.... Kind of hard for me to explain, though I'll instead show some example script ?

    PHP Code:
    .
    .
    $dom domxml_new_doc('1.0');
                
    $pageWidget = & new pageWidget(& $dom);
                
    $pageWidget -> create(); // page skeleton
                #
                
    $title = & new titleWidget(& $dom);
                
    $title -> create($action['PAGE.TITLE']);
                
    $pageWidget -> addHead($title -> fetch());
                
    $style = & new cssLinkWidget(& $dom);
                
    $style -> create($css);
                
    $pageWidget -> addHead($style -> fetch());
                
    #
                
    $pageGenerator = & new $this -> view(& $pageWidget, & $dom);
                
    #
                
    echo($pageWidget -> show());
    .

    Above, notice that I use references for the object pageGenerator ? If so, on executing this class the results from that class are automatically passed back to the class [snippet above] ready to be ECHOed;

    Whereas If I hadn't used references on the object pageGenerator and it's parameters then the content generated by this class [pageGenerator] wouldn't I believe be passed back automatically.

    Also on your second point about a class constructor ? You are correct

    Hope this has helped to explain references; maybe reading about references over at HarryF's site would also help you ?

    www.phppatterns.com

  3. #3
    SitePoint Zealot
    Join Date
    Aug 2003
    Location
    Singapore
    Posts
    105
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Still not sure, but do references help to increase efficiency?

    edit: after reading around, found out the advantages, but where would it's practical uses be? And can someone explain why would I return a reference? Thanks!
    Last edited by The Red Guy; Sep 26, 2003 at 18:09.

  4. #4
    Non-Member
    Join Date
    Jan 2003
    Posts
    5,748
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Umm... You do not return a reference; You just use it

    The

    PHP Code:
     
    in front of a variable as in my example script is the actual reference ? To your question of advantages I'd definitely say that there is an advantage when using Objects and Arrays as your most likely to be dealing with a large amount of data ?

    Better then to pass this data by references as apposed to passing by a copy of the data no ?

  5. #5
    Thinking about Visual Thinking
    Join Date
    May 2003
    Location
    back in South Africa
    Posts
    378
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    References are used in the following situations:
    • to avoid passing a copy (of a large amount) of data (see. expl. of Dr Livingston), hence the execution of the script is boosted
    • to be in the position to manipulate the original container of the data ... pay attention!

    Quote Originally Posted by Dr Livingston
    Umm... You do not return a reference; You just use it
    Well, acutally you can return a reference, if you define the signature of the function/method as:
    PHP Code:
    function & returnReference() {
       
       
    // do something and return a variable

    Take notice that in PHP5 objects are always passed by reference .

  6. #6
    Non-Member
    Join Date
    Jan 2003
    Posts
    5,748
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    if you define the signature of the function/method as
    Sorry, my mistake - Didn't realise that a reference could be returned ?

    Thanks Stefano F for pointing that out

  7. #7
    SitePoint Zealot
    Join Date
    Aug 2003
    Location
    Singapore
    Posts
    105
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Let's say I've a method getObject ($param), and another displayObject ($param). How do I make $param modify to a variable in a class, and then affect the other? Like if I input $param to be 'foo', then can I pass the reference to $this->param, then in turn the $param in displayObject() can be passed by reference instead? How do I do that? Thanks!

  8. #8
    SitePoint Zealot
    Join Date
    Aug 2003
    Location
    Singapore
    Posts
    105
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Also, can anyone explain what goes on in something like while (list($key,$value) = each($array))?

  9. #9
    ********* Member website's Avatar
    Join Date
    Oct 2002
    Location
    Iceland
    Posts
    1,238
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by The Red Guy
    Also, can anyone explain what goes on in something like while (list($key,$value) = each($array))?
    Then you are simply getting the key and value of the current index/key in the array and then incrementing it, it works just like
    PHP Code:
    foreach ($array as $key => $value) { } 
    some comments in the manual that can explain further.
    - website

  10. #10
    SitePoint Zealot
    Join Date
    Aug 2003
    Location
    Singapore
    Posts
    105
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by website
    Then you are simply getting the key and value of the current index/key in the array and then incrementing it, it works just like
    PHP Code:
    foreach ($array as $key => $value) { } 
    some comments in the manual that can explain further.
    Thanks! Then what are the differences between the two, in what situation is one preferred over the other? Also, regarding my question can someone answer it?
    Quote Originally Posted by The Red Guy
    Let's say I've a method getObject ($param), and another displayObject ($param). How do I make $param modify to a variable in a class, and then affect the other? Like if I input $param to be 'foo', then can I pass the reference to $this->param, then in turn the $param in displayObject() can be passed by reference instead? How do I do that? Thanks!

  11. #11
    Non-Member
    Join Date
    Jan 2003
    Posts
    5,748
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Umm.... If I remember properly from the manual ?

    PHP Code:
    .
    .
    list(
    $a$b$c$d) = each(mysql_fetch_array($sql));
    .

    This would, assume you have 4 fields in a table, put that particular row into the variables inside the list(...) no ?

    As to you original question it's too confusing for me to get my head around at the moment...

    I'm seriously lacking some decent sleep at the moment

  12. #12
    ********* Member website's Avatar
    Join Date
    Oct 2002
    Location
    Iceland
    Posts
    1,238
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Dr, I belive this does not work, or at least you wouldn't get the desired results.

    What does each() do?
    each() takes the element which the internal pointer of the array points to and returns its key and value as an array on the form of
    [0] => key
    [1] => value
    [key] => key
    [value] => value
    then it increments the internal pointer (then to the next element).

    Example:
    PHP Code:
    $array = array('value1''value2''key3' => 'value3');
    //now the pointer of the array points to the first element
    $arr1 each($array);
    /*
    print_r($arr1) would show something like:
    Array(
      [0] => 0
      [1] => value1
      [key] => 0
      [value] => value1
    )
    */
    $arr2 each($array);
    /*
    print_r($arr2) would show something like:
    Array(
      [0] => 1
      [1] => value2
      [key] => 1
      [value] => value2
    )
    */
    $arr3 each($array);
    /*
    print_r($arr3) would show something like:
    Array(
      [0] => key3
      [1] => value3
      [key] => key3
      [value] => value3
    )
    */ 
    As you see you could do the same with foreach().
    I have rarely used each() although it does happen, it could be useful when you need some for loop or something. Imagine something like this:
    PHP Code:
    for ($i 0$i count($arr); $i++) {
    list(
    $key$value) = each($arr);
    /* do something */ 
    As to what Dr Livingston was doing you would do it somehow like this:
    PHP Code:
    $sql 'SELECT col1, col2, col3 FROM table';
    $rslt mysql_query($sql);
    //because mysql_fetch_row returns the columns as keys in the array we'll use that (mysql_fetch_array can also be used);
    while (list($col1$col2$col3) = mysql_fetch_row($rslt)) { } 
    About the second question, I am not sure what you are asking but here is some code that might clear something?
    PHP Code:
    class Test {
      var 
    $test;

      function 
    assignTest($test) {
        
    $this->test =& $test;
      }

      function 
    printTest() {
        echo 
    $this->test;
      }
    }

    $test 'A';

    $obj = new Test();
    $obj->assignTest(&$test);
    $test 'B';
    $obj->printTest();
    //will echo B 
    Hope this helps...
    - website

  13. #13
    Non-Member
    Join Date
    Jan 2003
    Posts
    5,748
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    while (list($col1, $col2, $col3) = mysql_fetch_row($rslt)) { }
    Umm.... Sorry I couldn't exactly remember from what I had read in the manual ...

    If I remember properly from the manual ?
    I knew it was down that route, so to speak

  14. #14
    SitePoint Zealot
    Join Date
    Aug 2003
    Location
    Singapore
    Posts
    105
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I've one last query, for the code below I'm trying to evaluate whether the supplied parameter of a function exists in an array. Is this correct?

    PHP Code:
    function getProfile ($select_team='') {
            
            if (
    $select_team == (in_array($_GET['act'], $this->team))) {
    //do some code
            
    }
            else {
    //do some code
            
    }
            return 
    $this->select_team['results_sql'];
        } 

  15. #15
    Non-Member
    Join Date
    Jan 2003
    Posts
    5,748
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I just don't get your OOP -

    PHP Code:
    $this->team 
    I assume you are referring to a class property although there is no referrence to an object it's self yes ?

    Something is way wrong with your example...

  16. #16
    ********* Member website's Avatar
    Join Date
    Oct 2002
    Location
    Iceland
    Posts
    1,238
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by The Red Guy
    I've one last query, for the code below I'm trying to evaluate whether the supplied parameter of a function exists in an array. Is this correct?

    PHP Code:
    function getProfile ($select_team='') {
            
            if (
    $select_team == (in_array($_GET['act'], $this->team))) {
    //do some code
            
    }
            else {
    //do some code
            
    }
            return 
    $this->select_team['results_sql'];
        } 
    if $this->team is the array you are searching, $_GET['act'] is the string/number you are checking if exists in array and $select_team is a boolean variable it is ok.

    But if I understand you correctly you are checking if $select_team exists in the $this->teams array you should propably do something like
    PHP Code:
    if (in_array($select_team$this->team)) { } 
    But then I don't understand where $_GET['act'] comes into things. If you would describe your problem in more detail it would be easier for us to help you.
    - website

  17. #17
    SitePoint Zealot
    Join Date
    Aug 2003
    Location
    Singapore
    Posts
    105
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ah thanks, but I've edited out all the variables and such, there's a var $team = array(). I would like to take the value of $select_team parameter given to the function, and check whether it is in a database, which is specified in $team[]. Thus I want to check is viewteam.php?act=teamname exists by using $_GET['act']. Next I'd automate the process by checking against whether in_array returns true, so that I can know what query to execute based on the team specified.

  18. #18
    ********* Member website's Avatar
    Join Date
    Oct 2002
    Location
    Iceland
    Posts
    1,238
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    so you want to check first if $_GET['act'] exists, if it exists use it, else use the $select_team var that can be passed to the function, then compare that against the array?

    so something like:
    PHP Code:
    function getProfile ($select_team='') {
            
    $team = isset($_GET['act']) ? $_GET['act'] : $select_team;

            if ((
    in_array($team$this->team))) {
    //do some code
            
    }
            else {
    //do some code
            
    }
            return 
    $this->select_team['results_sql'];
        } 
    would do the trick?
    - website

  19. #19
    SitePoint Zealot
    Join Date
    Aug 2003
    Location
    Singapore
    Posts
    105
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks! I solved it using another method, got rid of some useless code. Once again thanks for your help!


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
  •