[PHP] re: Refering to an object from within an array variable in another class

2002-09-03 Thread Javier Montserat

Hi,

Syntax B doesnt seem to work exactly because as you say it doesn't directly 
access the 'bars' variable...;

here...

$reference_to_bar = $this-GetBar($id) ;

it seems that a copy of the bar object is being created, which is a new 
object.  This new bar object is not related to the bar object which is a 
part of the $bars array property of the foo object.

When you set the marklar property to the new value ...

$reference_to_bar-SetMarklar($value) ;

the value is changed for the new copy of the bar object you have just 
created.  Because this value is assigned to the new object it doesn't appear 
when you print the contents of foo.  This is illustrated by the lines i've 
added below.

Maybe after setting the new property value you could reassigned this object 
to the $bars array of object foo.

I'm no expert but it's always fun to play with code...

Hope this helps in some way,

Javier

/*
But this syntax doesn't: and this
would be prefered, as it doesn't directly
access the 'bars' variable...
*/

$reference_to_bar = $this-GetBar($id) ;
echo $reference_to_bar-marklar;
$reference_to_bar-SetMarklar($value) ;
echo $reference_to_bar-marklar;
print_r($reference_to_bar);


_
Chat with friends online, try MSN Messenger: http://messenger.msn.com


-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php




[PHP] re: Refering to an object from within an array variable in another class

2002-09-03 Thread Javier Montserat

I believe the following discussion from Web Application Development with PHP 
4.0 (available online at http://safari.oreilly.com) may shed some light on 
your question...



Whenever PHP encounters a statement for which write access to a variable is 
needed, it evaluates and calculates the data that will be written into the 
variable, copies it, and assigns the result to the target space in memory. 
(This description is a little bit simplified, but you get the idea.)


$some_var = 2;

$my_var = $some_var * 3;

$new_var = $some_var + $my_var;

Looking at this script, PHP will

Create space for $some_var and write 2 into it.

Create space for $my_var, retrieve the value of $some_var, multiply it by 3, 
and assign it to the newly allocated memory.

Create space for $new_var, retrieve the values of $some_var and $my_var, 
total them, and write them back to the new place in memory.

Well, this sounds nice and logical—but these are simple types and you've 
worked with them many times already. Things are very different (and 
illogical) when PHP is handling classes:


class my_class
{
   var $var1, $var2, $var3;
}

$my_object = new my_class;

$my_object-var1 = 1;
$my_object-var2 = 2;
$my_object-var3 = 3;

$new_object = $my_object;

$new_object-var1 = 3;
$new_object-var2 = 2;
$new_object-var3 = 1;

print(My object goes $my_object-var1, $my_object-var2, $my_object-var3 
!br);
print(New object goes $new_object-var1, $new_object-var2, 
$new_object-var3 !br);

What do you think this will produce as output? The script first declares a 
class, creates one instance of it, and assigns values to its three 
properties. After having done this, it creates a new reference to this 
object, reassigns its properties, and then just prints out each property by 
using both references. Remember, one instance. Figure 2.7 shows the result.

Figure 2.7. PHP creating a copy instead of a reference.

If you're not surprised now, you either know PHP very well already or 
haven't thought enough about objects yet. PHP has created a copy, a new 
instance of my_class instead of just a new reference! This is not the 
desired behavior, since the new operator is supposed to create an instance 
of my_class in memory, returning a reference to it. Thus, when assigning 
this reference to another variable, only the reference should be copied, 
leaving the original data untouched—similar to a file system link, which 
allows access to the same data through different locations on the file 
system. This behavior of PHP—creating copies of referenced data rather than 
just the reference itself—may not sound important enough to focus on; 
however, you'll see in a moment that it actually does make a difference.

Note:   At the time of this writing, both PHP 3.0 and PHP 4.0 are using copy 
syntax. A chat with someone closely involved in the core development 
revealed that the plan is to change the default behavior to use a reference 
syntax, but this change would cause a loss of backward compatibility. A 
possible change is planned with version 4.1—if this happens, information 
contained here will be invalid for all future versions.

Web Application Development with PHP 4.0

Tobias Ratschiller
Till Gerken
Zeev Suraski
Andi Gutmans
Publisher: New Riders Publishing
First Edition July 12, 2000
ISBN: 0-7357-0997-1, 416 pages



_
Send and receive Hotmail on your mobile device: http://mobile.msn.com


-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php