php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #53720 Access variable in class dynamicaly
Submitted: 2011-01-12 13:14 UTC Modified: 2011-01-12 21:26 UTC
From: kh dot wild at wicom dot li Assigned:
Status: Not a bug Package: Scripting Engine problem
PHP Version: 5.3.5 OS: freebsd, osx
Private report: No CVE-ID: None
 [2011-01-12 13:14 UTC] kh dot wild at wicom dot li
Description:
------------
It seems, that when I read a class variable with the varname in a string, it 
works, but writing in the same way doesn't

Test script:
---------------
<?
class XXX 
{
	private $cisupport = Array( 1 );
	private $cicoordinator = Array();
	
	function support() {
		$x = 'cisupport';
		print( "Request support static:\n");
		print_r( $this->cisupport );
		print( "Request support dynamic:\n");
		print_r( $this->$x );
	}
	function coordinator() {
		$x = 'cicoordinator';
		print( "Set coordinator static:\n");
		$this->cicoordinator[ 1 ] = 12;
		print_r( $this->cicoordinator );
		print( "Set coordinator dynamic:\n");
		$this->${x}[ 1 ] = 9999;
		print_r( $this->${x} );
		
	}
}

$x = new XXX;
$x->support();
$x->coordinator();

?>

Expected result:
----------------
Request support static:
Array
(
    [0] => 1
)
Request support dynamic:
Array
(
    [0] => 1
)
Set coordinator static:
Array
(
    [1] => 12
)
Set coordinator dynamic:
Array
(
    [1] => 9999
)

Actual result:
--------------
Request support static:
Array
(
    [0] => 1
)
Request support dynamic:
Array
(
    [0] => 1
)
Set coordinator static:
Array
(
    [1] => 12
)
Set coordinator dynamic:
Array
(
    [1] => 12
)

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2011-01-12 21:25 UTC] felipe@php.net
-Status: Open +Status: Bogus
 [2011-01-12 21:25 UTC] felipe@php.net
Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at
http://www.php.net/manual/ and the instructions on how to report
a bug at http://bugs.php.net/how-to-report.php

This is expected, see:

When you do:
$x = 'cicoordinator';
$this->${x}[ 1 ] = 9999;

You are doing '$this->i = 9999;', because ${x}[1] == 'i' (c[i]coordinator)
 [2011-01-12 21:26 UTC] felipe@php.net
-Package: Reflection related +Package: Scripting Engine problem
 [2011-01-13 08:06 UTC] kh dot wild at wicom dot li
Thank you very much. I've got it.

$this->{$x}[ 1 ] = 9999;

is working.
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Sun Jul 20 21:00:02 2025 UTC