php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #43022 Globals are not set if object passed by reference
Submitted: 2007-10-18 14:24 UTC Modified: 2007-10-24 06:49 UTC
From: sonya at look-for-it dot de Assigned: dmitry (profile)
Status: Not a bug Package: SOAP related
PHP Version: 5.2CVS-2007-10-22 OS: Windows XP
Private report: No CVE-ID: None
 [2007-10-18 14:24 UTC] sonya at look-for-it dot de
Description:
------------
I have problem with $GLOBALS while executing scripts via SOAP Service. Objects passed by reference is not set in $GLOBALS and cannot be used in other classes. The same code works perfectly if executed direct in browser.

Reproduce code:
---------------
client.php
----------------------------------------------
ini_set("soap.wsdl_cache_enabled", "0");
 $client = new SoapClient("test.wsdl", array('trace'=> 1, 'exceptions' => 0));
print $client->testGlobals();

--------------------------------------------
server.php
----------------------------------------------
function testGlobals() {
	global $TestClass1, $TestClass2;
	require_once 'inc.php';
	return $TestClass2->testvar2;
}
ini_set("soap.wsdl_cache_enabled", "0");
$server = new SoapServer("test.wsdl");
$server->addFunction("testGlobals");
$server->handle();

--------------------------------------------
inc.php
--------------------------------------------
require_once('TestClass1.class.php');
require_once('TestClass2.class.php');
$TestClass2 = new TestClass2();

-------------------------------------------
TestClass1.class.php
-------------------------------------------
class TestClass1 {

	public $testvar1;
	function TestClass1() {
		$this->testvar1 = 100;
	}
}

$TestClass1 = & new TestClass1();

---------------------------------------------
TestClass2.class.php
---------------------------------------------
class TestClass2 {

	public $testvar2;
	function TestClass2() {
		global $TestClass1;
		$this->testvar2 = $TestClass1->testvar1;
	}
}




Expected result:
----------------
I expect the value 100 to be returned by service. 

Actual result:
--------------
Nothing is returned. The reason is that in TestClass2 there is no $GLOBALS['TestClass1'] unless I pass $TestClass1 by value:
(last line of TestClass1.class.php):
$TestClass1 = new TestClass1();

What is wrong here?

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2007-10-19 08:27 UTC] sonya at look-for-it dot de
I have just tested it on Apache/1.3.33 (Debian GNU/Linux) and PHP/5.2.3. Same behaviour.
 [2007-10-22 11:05 UTC] sonya at look-for-it dot de
Thank you. I used the link you gave for windows (zip). But the objects passed by reference are still not saved in $GLOBALS. For the above example the object $TestClass1 is not saved in $GLOBALS['TestClass1'] when passed by reference and therefore it can not be access in TestClass2. 

global $TestClass1 <- this line doesn't point to the object unless I pass the object by value.
 [2007-10-23 09:28 UTC] jani@php.net
Assigned to the SOAP extension maintainer.
 [2007-10-24 06:49 UTC] dmitry@php.net
Your expectation is wrong and report is not releated to SOAP at all.
It is related to "global" and references.
The following script does exactly the same.

<?php
class TestClass1 {
	public $testvar1;
	function TestClass1() {
		$this->testvar1 = 100;
	}
}

class TestClass2 {
	public $testvar2;
	function TestClass2() {
		global $TestClass1;
		$this->testvar2 = $TestClass1->testvar1;
	}
}

function testGlobals() {
	global $TestClass1, $TestClass2;
	$TestClass1 =& new TestClass1(); // breaking reference
	$TestClass2 = new TestClass2();
	return $TestClass2->testvar2;
}

var_dump(testGlobals());
?>

global $TestClass1; - $TestClass1 become a reference to a global variable
$TestClass1 =& ...  - $TestClass1 become a reference to assigned object (the global variable is not affected by such assignment)
 
Thank you for report anyway.
 
PHP Copyright © 2001-2022 The PHP Group
All rights reserved.
Last updated: Thu Jan 27 21:03:35 2022 UTC