Naming vars from vars

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
BDKR
DevNet Resident
Posts: 1207
Joined: Sat Jun 08, 2002 1:24 pm
Location: Florida
Contact:

Naming vars from vars

Post by BDKR »

OK, this is going to sound stupid, but as I've never had occasion to do this, I'm now lost. It's prolly somethin' extremely easy.

What I am doing is parsing a line (string) that is ";" demilited. I am using explode() to seperate the fields. Now the information is used during the instantiation of db connection objects. The first parameter is what I want to name the object. So, the array is named $params. I want to use $param[0] to name the db object. So if $param[0] = Liberace, I want the to name the object $Liberace.

If there is a way to do this, I'd love to know.

Thanx,
BDKR (TRC)
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

You want to take a loop at variable variables I'd post an example but I always manage to get myself really confused with these although it tends to work in the end.

Mac
User avatar
hob_goblin
Forum Regular
Posts: 978
Joined: Sun Apr 28, 2002 9:53 pm
Contact:

Post by hob_goblin »

maybe something like

Code: Select all

<?
$&#123;$param&#1111;0]&#125; = $param&#1111;1];
?>
???
User avatar
BDKR
DevNet Resident
Posts: 1207
Joined: Sat Jun 08, 2002 1:24 pm
Location: Florida
Contact:

Post by BDKR »

Being that this is being done inside a loop, I don't think using variable vars will work. At each iteration through the loop, the value of $param[0] is going to change.

Code: Select all

# Now open the configuration file
$config_file=file("config.php");

# Iterate through the lines in the file and create the db objects
while(list($key, $val)=each($config_file))
  &#123;
  $params=explode(";", $val);
  $$params&#1111;0] = new Database('$params&#1111;1]');
  &#125;
So what's going to happen to the name of that object I created the second time through the loop? I'm going to try it anyways and see, but I suspect this isn't going to work.

Later on,
BDKR (TRC)
User avatar
BDKR
DevNet Resident
Posts: 1207
Joined: Sat Jun 08, 2002 1:24 pm
Location: Florida
Contact:

Post by BDKR »

OK. I didn't really fix it as much as I came up with a different approach. The first thing I did was change the object just a tad bit to include a var called $this->object_name. Then passed it it's name during instantiation.

Now I have an indexed array of connection objects wich when you get down to is is prolly better than an associative array. However, as I still need to know which system each object is associated with, I can just add a method to the object to identify itself with the $this->object_name var.

Yeeeeehawww Fool!

BDKR (TRC)
Post Reply