Is it possible to do something similar to the using
keyword in C# (and probably others) to limit variable scope? I'm experimenting with database connection patterns, and am currently trying to get this to work:
$db = array(
"server" =>"localhost",
"user" =>"root",
"pass" =>"my_password",
"database" =>"my_database"
);
$pdo = null;
{ // ??? These seem to be completely ignored, no errors, no effect at all
extract($db);
$pdo = new PDO("mysql:host=$server;dbname=$database", $user, $pass);
}
//Do database stuff
I'm using extract
, which is normally a bad idea, so I'm trying to protect whatever it returns where those curly braces are. In C# I could probably do something like using (extract($db)) { ... }
and whatever extract
returns would be limited to that scope, but I can't figure out if this is possible in PHP. I'm not even sure if PHP disposes of variables.
Any insight to this problem is much appreciated!