Skip to content

Instantly share code, notes, and snippets.

@johanbrook
Created February 23, 2011 18:34
Show Gist options
  • Save johanbrook/840900 to your computer and use it in GitHub Desktop.
Save johanbrook/840900 to your computer and use it in GitHub Desktop.
<?php
class User{
public $user_email = "[email protected]";
}
$obj = new User();
function return_stuff($key){
return $obj->$key;
}
echo return_stuff("user_email");
// Solution: store $key in temporary variable:
class User2{
public $mail = "[email protected]";
public $name = "johan";
}
function return_stuff($key){
$obj = new User2();
$ret = $key;
return $obj->$ret;
}
echo return_stuff("name");
// Solution 2: even no need for temp variable, just place $key inside brackets
class User3{
public $mail = "[email protected]";
public $name = "johan";
}
function return_stuff($key){
$obj = new User3();
return $obj->{$key};
}
echo return_stuff("name");
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment