SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Evangelist cyngon's Avatar
    Join Date
    Aug 2001
    Location
    Livonia, MI, USA
    Posts
    513
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    A few PHP reference questions

    I read HarryF's article entitled "PHP and Variable References" and I have a few questions.

    Note: it's good practice to use references when creating objects as well. So what we should have done in our example was;
    PHP Code:
    $data=& new Data// Create an instance of Data
    $output=& new Output ($data); // Pass it to an instance of Output

    // Use ControlPanel to modify some settings
    $cp=& new ControlPanel($data); 
    Although is doesn't make any difference in this example, when using more complex applications (or in particular third party class libraries) we may run into situations where there is alot happening behind the scenes. Unless specifically told otherwise, if in doubt use references.
    What is the difference between using = or =& when creating objects?


    Also, I have a question about the final example in the article:

    Returning References

    It's worth being aware that we can return references to values by placing the reference operation in the function declaration, e.g.;
    PHP Code:
    function & timesTwo ($number) {
        return 
    $number 2;

    Using an example with objects this might be;
    PHP Code:
    <?php
    class Message {
        var 
    $msg 'Hello World';
        function 
    setMsg ($msg) {
            
    $this->msg=$msg;
        }
        function 
    getMsg () {
            return 
    $this->msg;
        }
    }

    class 
    MessageFactory {
        
    // Return a reference to an instance of Message
        
    function & createMessage () {
            return new 
    Message;
        }
    }

    $factory= new MessageFactory;
    $msg=$factory->createMessage();
    echo (
    $msg->getMsg());
    ?>
    On the line:
    PHP Code:
    $msg=$factory->createMessage(); 
    What would happen if instead one wrote:
    PHP Code:
    $msg =& $factory->createMessage(); 
    Any help on these two questions would be greatly appreciated.

  2. #2
    SitePoint Wizard gold trophysilver trophy
    Join Date
    Nov 2000
    Location
    Switzerland
    Posts
    2,479
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quick answers right now (I'll see if I can come up with some code some other time that demonstrates the point)

    What is the difference between using = or =& when creating objects?
    Same argument as before basically - if you have a complicated set of classes working together, if you instantiate a class with just $ref= new Class; how do you know that some other object doesn't modify the instance of Class "behind the scenes"?

    Unless you know all the code intimately (and even then you might make mistakes), using a reference when creating an object is a good way to always be sure.

    With Java, for example, you only ever have references to objects when you create them.

    In PHP 5.0 the default behaviour will always be to create a reference, creating a copy being something that will be a special case you have to specifically identify.

    As to this -

    PHP Code:
    $msg =& $factory->createMessage(); 
    Having thought about it - I should update the article to do that, because

    PHP Code:
    $msg=$factory->createMessage(); 
    Makes $msg a copy again.

    If you're interested in an example where return references is useful, eZ publish (2.x) uses this frequently when SELECTing a group of records from a database. They simply select the ID of each record then use it to create objects for each record, using another select based on the ID, that fetches all the columns for that record. I used their approach here - see the getAll() method...

  3. #3
    SitePoint Evangelist cyngon's Avatar
    Join Date
    Aug 2001
    Location
    Livonia, MI, USA
    Posts
    513
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for the reply, HarryF.

    I still am quite fuzzy about = versus =& when instantiating classes. I think some sample code that shows how objects behave differently depending on which operator you used when creating then would help a lot.

    Good article though, very informative. Thanks.


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
  •