Jump to content

Accessing Class Arrays Dynamically


geomagnet

Recommended Posts

Hi,I want to access a class array that is called by another function. Here what I'm doing.

class myClass{var $myArray = Array(0=>Array("hello", "world",0),1=>Array("foo", "bar",1),2=>Array("voo", "doo",2));var $myVar = "HELLO WORLD";}function myFunc(){$theClass = new myClass();$i = 1;$test_0 = "myArray[1][0]";$test_1 = "myVar";$test_2 = "myArray[1][$i]";echo $theClass->myArray[0][0];echo $theClass->myArray[0][$i];echo $theClass->myVar;echo $theClass->$test_1;echo $theClass->$test_0;echo $theClass->$test_2;}myFunc();

Outputs:helloworldHELLO WORLDHELLO WORLDnothingnothingIs there a way to make this work so that class members can be accessed dynamically?ThanksJames

Link to comment
Share on other sites

There might be a way to make it work with variable variables, but I don't know it. I figure there's bound to be a different way of accomplishing your goal, but since I don't know what your goal is exactly, I can't guess at that either.But if you keep going in this direction, then the following works. It's ugly, but it works.

$s = '$theClass->' . "$test_2";$s = eval ("return $s;");echo $s;

Link to comment
Share on other sites

There might be a way to make it work with variable variables, but I don't know it. I figure there's bound to be a different way of accomplishing your goal, but since I don't know what your goal is exactly, I can't guess at that either.But if you keep going in this direction, then the following works. It's ugly, but it works.
$s = '$theClass->' . "$test_2";$s = eval ("return $s;");echo $s;

That might work. I've written a simple parsing script that allows me to parse HTML documents with regex. The identifiers are theclass members surrounded with {} brackets. Works really well for variables, but not arrays.
$content = file_get_contents($file);preg_match_all("/\{(\w+)\}/",  $content, $reg);//preg_match_all("/\{((\w+)(\[[0-9]\])*)\}/",  $content, $reg); // finds square brackets, but fails to process.$arr_match = array();$pattern = array();		foreach($reg[1] as $arr)		{						array_push($arr_match, $var->$arr);			array_push($pattern, "/\{".$arr."\}/");		}$content = preg_replace($pattern, $arr_match , $content);	$content = str_replace("\r","", $content);$content = str_replace("\n","",$content);return addslashes($content);}

So passing the function a class will sort out and replace any pertain values in html...parseTemplate( "../status_".$status[1].".html", $icons); // status[1] is the HTML template to be used based on the users progression.of course the $icons class has all the different paths and lables (some) in the form of an array.

<div id="status_container_{task_id}"><table class="control_div_150" style="right:-5px">	 <tr>		 <td class="taskHead"><div style="float:right">			<img src="../images/Play1Hot.png" id="hide_status_container_{task_id}"></div>				 Task Status				</td>		   </tr>  <tr>   <td><img id="icon_15_{task_id}_4" src="{icon_path}{image_15}" /> {text_15}</td>   </tr>    	<tr>   <td><img id="icon_16_{task_id}_4" src="{icon_path}{image_16}"/> {text_16}</td>   </tr>    	<tr>   <td><img id="icon_17_{task_id}_4" src="{icon_path}{image_13}" /> {text_17}</td>   </tr>    	<tr>	  <td><img id="icon_5_{task_id}_4" src="{icon_path}{image_5}" /> {text_5}</td>	</tr>	</table>	</div>

Every {string} is found with regex and replaced with the corresponding class member's value.{task_id} finds and replaces $icons->task_id in the appropriate place. {image_5} and {text_5} is mycurrent work around, but would rather it work like {image[5]} which translates to $icons->image[5]As you have previously seen in my first example
$a = "image[5]"
(string representation of an array element)
does not work
as
$icons->$a.
however,
$a = "task_id"
(string representation of a variable name) will work as
$icons->$a
and return the value of
$icons->task_id
.I did try the eval but not the same way.
eval("$icons->$a")
does not solve the problem.I'll try out your suggestion and let you know what happens.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...