• You are not logged in. | Login

Post a reply

March 14, 2007 10:46 am

mellis
Member
Ranks

Difficulties with links to objects

I’m terribly confused. Who can tell me if this code contains any bugs?

<?php 

class Simple 
{ 
    public $name; 
} 

$obj1 = new Simple(); 
$obj1->name = 'obj1'; 

$obj2 = new Simple(); 
$obj2->name = 'obj2'; 

$ref1 = $obj1; 

$ref2 = $ref1; 

$ref1 = $obj2; 

print 'name: '.$ref2->name; // output name: obj1 

?>

I’ve examined manual but haven’t found any explanation.

P.S. PHP 5.2


 

 

March 14, 2007 10:48 am

sirburpsalot
Member
Ranks

Re: Difficulties with links to objects

$ref1 = $obj1; // $ref1 is a link to obj1

$ref2 = $ref1; // $ref2 is a link to obj1

$ref1 = $obj2; // $ref1 is a link to obj2

Everything seems to be all right to me.


 

 

March 14, 2007 10:50 am

mellis
Member
Ranks

Re: Difficulties with links to objects

$ref1 = $obj1; // $ref1 is a link to obj1

If we follow the manual strictly, we’ll see that both $ref1 and $obj1 are links to the same content. After  $ref2 = $ref1 another link to the same content should be added.

Thus after $ref1 = $obj2 all three links should indicate to $obj2.

In the manual it is written that links in PHP are different from indicators in C. At this there is no such type as ‘link’ in PHP and so it’s impossible to check what $ref2 is.

Generally basing on the work of the template we may say that PHP differentiates links types.


 

 

March 14, 2007 10:52 am

mmwfan
Member
Ranks

Re: Difficulties with links to objects

$ref1 = obj1;
ref1 -> obj1;

$ref2 = $ref1;
ref2 -> obj1 instead of ref2 -> ref1 -> obj1;

$ref1 = $obj2;
ref1 -> obj2, ref2 isn’t involved


 

 

March 14, 2007 1:18 pm

mellis
Member
Ranks

Re: Difficulties with links to objects

Now I’ll try to formulate more precisely what exactly embarrasses me.

<?php 
class Simple 
{ 
    public $name; 
} 

$obj1 = new Simple(); 
$obj1->name = 'obj1'; 

$obj2 = new Simple(); 
$obj2->name = 'obj2'; 

$ref1 = $obj1; 

$ref1 = $obj2; 

print 'name: '.$obj1->name; // "name: obj1" 
?>

As it is known objects in PHP5 are transmitted via link i.e. string $ref1 = $obj1 is equal to $ref1 =& $obj1. We’ll try to replace this string:

<?php 
$ref1 =& $obj1; 

$ref1 = $obj2; 

print 'name: '.$obj1->name; // in this case "name: obj2" 
?>

Strings $ref1 = $obj1 and $ref1 =& $obj1 turn out not to be equal.

We’ll try to change the following string:

<?php 
$ref1 =& $obj1; 

$ref1 =& $obj2; 

print 'name: '.$obj1->name; // in this case "name: obj1" 
?>

It is quite logical because it has to be so.

Generally speaking in case with objects links act according to some other rules which are not described in the manual or maybe I have missed something…


 

 

March 14, 2007 1:24 pm

Conker
Member
Ranks

Re: Difficulties with links to objects

As it is known objects in PHP5 are transmitted via link

Not everyone knows it… You’d better cite the manual.


 

 

March 14, 2007 1:27 pm

mellis
Member
Ranks

Re: Difficulties with links to objects

OK, I’ve found. It has been already discussed here http://www.php.net/manual/en/language.oop5.basic.php


 

 

March 14, 2007 1:31 pm

Re: Difficulties with links to objects

mellis, where is such behavior discussed? Show me the link, please.


 

 

March 14, 2007 1:38 pm

mellis
Member
Ranks

Re: Difficulties with links to objects

In the unit ‘new’

<?php 
$assigned  =  $instance; 
$reference =& $instance; 

$instance->var = '$assigned will have this value'; 

$instance = null; // $instance and $reference become null 

var_dump($instance); 
var_dump($reference); 
var_dump($assigned); 
?>

The above example will output:

NULL 
NULL 
object(SimpleClass)#1 (1) { 
   ["var"]=> 
     string(30) "$assigned will have this value" 
}

Also
http://www.php.net/manual/en/language.o … .php#50469
http://www.php.net/manual/en/language.o … .php#64410
In the unit "User Contributed Notes"


 

 

March 14, 2007 1:41 pm

Re: Difficulties with links to objects

More detailed about links and objects you may read here - http://blog.libssh2.org/index.php?/...g-lied-to..html


 

 
  • Actions
  • Top
ITCrimea. Ukraine Web Development Company. Professional Developers and Web Designers Team
Custom Web Designs, Internet Applications, E-Commerce Websites, Interactive Sites, Database-Driven Sites and Services