Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- class User{
- public $user_email = "johan@mail.com";
- }
- $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 = "johan@mail.com";
- 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 = "johan@mail.com";
- public $name = "johan";
- }
- function return_stuff($key){
- $obj = new User3();
- return $obj->{$key};
- }
- echo return_stuff("name");
- ?>
Add Comment
Please, Sign In to add comment