php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #23582 Objects and handles
Submitted: 2003-05-11 05:18 UTC Modified: 2003-05-11 05:34 UTC
From: philip dot nilsson at chello dot se Assigned:
Status: Not a bug Package: Class/Object related
PHP Version: 4.3.2RC2 OS: Windows XP
Private report: No CVE-ID: None
 [2003-05-11 05:18 UTC] philip dot nilsson at chello dot se
This bug resembles http://bugs.php.net/bug.php?id=23576, but I am not really sure if they are the same.

First, I'll show a working(or not working, :)) example:
--------
class CTest {
	var $filename; var $file;
	function CTest() { $this->$filename = 'data/data.txt'; }
	function Write($data1, $data2) {
		$data[] = array($data1, $data2);
		$this->$file = fopen($this->$filename, 'w');
		for ($i = 0; $i < count($data); $i++)
			fwrite($this->$file, implode('||', $data[$i]));
		fclose($this->$file);
	}
}
$test = new CTest();
print('<pre>'); print_r($test); print('</pre>');
$test->Write('data', 'moredata');
print('<pre>'); print_r($test); print('</pre>');
--------

And the output:

--------
ctest Object
(
    [filename] => 
    [file] => 
    [] => data/data.txt
)

ctest Object
(
    [filename] => 
    [file] => 
    [] => Resource id #1
)
--------

And the output I expected:

--------
ctest Object
(
    [filename] => data/data.txt
    [file] => empty, maybe?
)

ctest Object
(
    [filename] => data/data.txt
    [file] => Resource id #1
)
--------

As you can see the class members gets changed. If I remove the constructor, the empty [] variable disappears, but the variables still gets messed up. This behaviour is completely unexpected, and also totally illogical. I don't think this bug affects objects that do not use handles, but it might. And I haven't tested it on Linux yet, but I do not think it would work there either.

This bug has been in PHP at least a couple versions as I remember it from about a year ago, I tried to make a class that would handle my database connections, but the handle got invalid after the first use. Fixing it would be really nice.

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2003-05-11 05:34 UTC] wez@php.net
$this->$file should be $this->file.
Please read the section on variable variables in the manual.
User error -> bogus.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Apr 18 03:01:28 2024 UTC