Created
February 23, 2011 18:34
-
-
Save johanbrook/840900 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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