SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Zealot
    Join Date
    Jan 2003
    Location
    New York, NY
    Posts
    125
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Handling dynamically allocated memory

    If I do this:
    PHP Code:
      $array[$i] = new Object();
      
    $objPtr $array[$i]; 
    Does $objPtr point to the same instance of Object as $array[$i], or is a copy created?
    So I says to him, I says, Hey! Llama! How about a little something, you know, for the effort, you know?

  2. #2
    ********* Callithumpian silver trophy freakysid's Avatar
    Join Date
    Jun 2000
    Location
    Sydney, Australia
    Posts
    3,798
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    in PHP 4 a copy of the object will be created by your code. You would need to explicity create a reference to the object using:

    $objPtr = &$array[$i];

    Please note that this applies also when passing an object to a method of another object or as a parameter in a function call.

    Example

    $objectA->methodA(&$objectB);

    AFAIK this will all change in php 5 when php will be more javalike in that objects will be implicitly assigned and passed by reference.

  3. #3
    SitePoint Zealot
    Join Date
    Jan 2003
    Location
    New York, NY
    Posts
    125
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks. I'm going to have to remember that (at least until PHP 5 is stable). Having a background in Java, I would just automatically assume that it works the other way around.
    So I says to him, I says, Hey! Llama! How about a little something, you know, for the effort, you know?

  4. #4
    SitePoint Zealot
    Join Date
    Jan 2003
    Location
    New York, NY
    Posts
    125
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    $objectA->methodA(&$objectB);
    If methodA is defined as:

    PHP Code:
     class ObjectA {
         
    methodA(&$obj) { 
             
    // code
         
    }
     } 
    Do I still need to call it the way you demonstrated, or can I use

    PHP Code:
     $objectA->methodA($objectB); 
    ?
    So I says to him, I says, Hey! Llama! How about a little something, you know, for the effort, you know?

  5. #5
    ********* Callithumpian silver trophy freakysid's Avatar
    Join Date
    Jun 2000
    Location
    Sydney, Australia
    Posts
    3,798
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'm not sure - I don't think so. Hopefully an OO guru will respond. I'm not much of a OO programmer. I code like I make love - quick and dirty.

    I should just point out that the topic of this thread is a little misleading because php is an interpreted language all memory is allocated dynamically, and the interpreter uses run-time optimisations such as reference counting which are hidden from the coder. The topic would relate to compiled languages such as C++ and Java where memory is either allocated to an object at compile time or at run time (dynamically). I say that because I think many readers here would not understand the topic otherwise.

  6. #6
    SitePoint Zealot
    Join Date
    Jan 2003
    Location
    New York, NY
    Posts
    125
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by freakysid
    I should just point out that the topic of this thread is a little misleading because php is an interpreted language all memory is allocated dynamically.
    You're right. I have to admit, I may be coding in PHP, but I'm thinking in C++.
    So I says to him, I says, Hey! Llama! How about a little something, you know, for the effort, you know?


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
  •