php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #28106 New object model fails where old style works fine
Submitted: 2004-04-22 13:34 UTC Modified: 2005-01-18 01:00 UTC
Votes:8
Avg. Score:4.8 ± 0.7
Reproduced:5 of 5 (100.0%)
Same Version:2 (40.0%)
Same OS:1 (20.0%)
From: kupka at learninglab dot de Assigned:
Status: No Feedback Package: Scripting Engine problem
PHP Version: 5.0.0RC3 OS: Mac OS X
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: kupka at learninglab dot de
New email:
PHP Version: OS:

 

 [2004-04-22 13:34 UTC] kupka at learninglab dot de
Description:
------------
You have a main object which includes some other 
objects. This object is stored in the session. The 
other objects have a reference to the main object. If 
you start a new session these references should point to 
the main object. If you pass the objects to the main 
object in the old style with "&" this works fine. If you 
use the new style without "&" it doesn't work as 
exspected.

Reproduce code:
---------------
The classes:

class CHello
{
	var $store;

	// this doesn't work as exspected
	public function CHello($store)
	{
		$this->store = $store;
	}
/*
	// this works fine
	public function CHello(&$store)
	{
		$this->store =& $store;
	}
*/
	public function hello( )
	{
		$v = $this->store->value;
		echo "Hello World: value is $v. (store id: $this->store)<br>";
	}
}

class CStore 
{
	var $object1;
	var $object2;

	var $value = 0;
}

---------------------------

The first script:

session_start();

$store = new CStore;
$hello1 = new CHello($store);
$hello2 = new CHello($store);

$store->value = 999;
$store->object1 = $hello1;
$store->object2 = $hello2;

echo "store id: $store<br>";

$store->object1->hello();
$store->object2->hello();

session_register("store");

---------------------------

The second script:

session_start();

$store->value = 123;

echo "store id: $store<br>";

echo $store->object1->hello();
echo $store->object2->hello();


Expected result:
----------------
The first script where the objects are generated:
store id: Object id #4
Hello World: value is 999. (store id: Object id #4)
Hello World: value is 999. (store id: Object id #4)

The second script:
store id: Object id #1
Hello World: value is 123. (store id: Object id #1)
Hello World: value is 123. (store id: Object id #1)

Actual result:
--------------
The first script where the objects are generated:
store id: Object id #6
Hello World: value is 999. (store id: Object id #6)
Hello World: value is 999. (store id: Object id #6)

The second script:
store id: Object id #1
Hello World: value is 999. (store id: Object id #3)
Hello World: value is 999. (store id: Object id #5)

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2004-04-25 16:48 UTC] kupka at learninglab dot de
The second script:

session_start();

$store->value = 123;

echo "store id: $store<br>";

$store->object1->hello();
$store->object2->hello();
 [2004-04-26 10:31 UTC] kupka at learninglab dot de
In PHP 5 Release Candidate 2 the problem still exists. 
The second script generates three different CStore 
objects, but there should only be one CStore objec and 
the two CHello objects should have a pointer to the same 
CStore object.
 [2004-05-08 10:27 UTC] antonr at game dot permonline dot ru
5.0.0 RC2, Win32

----------------------------------
<?php
  class MyClass
    { public $number;
      function EchoNum()
        { echo $this->number;
          echo "<br>";
        }
    }

  $a = new MyClass;
  $a->number = 1;

  $b = $a;
  $b->number = 2;

  $a->EchoNum();
  $b->EchoNum();
?>

outputs:

2
2

----------------------------------
but...

<?php
  class MyClass1
    { public $myclass2;
    }

  class MyClass2
    { public $myclass1;
    }

  $a = new MyClass1;
  $a->myclass2 = new MyClass2;

  $a->myclass2->myclass1 = $a;
  $a->number = 1;
  echo $a->myclass2->myclass1->number;
  echo "<br>";

  $c = serialize($a);
  echo $c;

  echo "<br><br>";

  $b = unserialize($c);

  $b->number = 2;
  echo $b->number;
  echo "<br>";
  echo $b->myclass2->myclass1->number;
  echo "<br>";
  echo "<br>";
?>

outputs:

1
O:8:"MyClass1":2:{s:8:"myclass2";O:8:"MyClass2":1:{s:8:"myclass1";O:8:"MyClass1":2:{s:8:"myclass2";r:2;s:6:"number";i:1;}}s:6:"number";i:1;}

2
1
----------------------------------

while: 

<?php
  class MyClass1
    { public $myclass2;
    }

  class MyClass2
    { public $myclass1;
    }

  $a = new MyClass1;
  $a->myclass2 = new MyClass2;

  $a->myclass2->myclass1 = &$a;  <--- ampersand symbol added
  $a->number = 1;
  echo $a->myclass2->myclass1->number;
  echo "<br>";

  $c = serialize($a);
  echo $c;

  echo "<br><br>";

  $b = unserialize($c);

  $b->number = 2;
  echo $b->number;
  echo "<br>";
  echo $b->myclass2->myclass1->number;
  echo "<br>";
  echo "<br>";
?>

works fine and outputs:

1
O:8:"MyClass1":2:{s:8:"myclass2";O:8:"MyClass2":1:{s:8:"myclass1";r:1;}s:6:"number";i:1;}

2
2
 [2004-05-08 10:32 UTC] antonr at game dot permonline dot ru
So, there is a problem with serialization and deserialization of objects with references to others objects.
 [2004-06-10 12:23 UTC] kupka at learninglab dot de
In PHP 5 Release Candidate 3 the problem still exists.
 [2004-07-12 20:00 UTC] antonr at game dot permonline dot ru
to kupka at learninglab dot de:
please check your script with last php5 snapshots... i suppose this bug was corrected
 [2005-01-10 15:20 UTC] sniper@php.net
Please try using this CVS snapshot:

  http://snaps.php.net/php5-latest.tar.gz
 
For Windows:
 
  http://snaps.php.net/win32/php5-win32-latest.zip


 [2005-01-18 01:00 UTC] php-bugs at lists dot php dot net
No feedback was provided for this bug for over a week, so it is
being suspended automatically. If you are able to provide the
information that was originally requested, please do so and change
the status of the bug back to "Open".
 [2006-03-26 04:23 UTC] wagner at weblife dot com dot br
Maybe, more than one class's do not to be there with others in a same document. . .i have this problem in my implamentation...

Object id #4 -> when i set the class.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Wed Apr 24 06:01:29 2024 UTC