Guest User

Untitled

a guest
Jul 23rd, 2018
274
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.66 KB | None | 0 0
  1. <?php
  2.  
  3. class User{
  4. public $user_email = "johan@mail.com";
  5. }
  6.  
  7. $obj = new User();
  8.  
  9. function return_stuff($key){
  10. return $obj->$key;
  11. }
  12.  
  13.  
  14. echo return_stuff("user_email");
  15.  
  16.  
  17.  
  18. // Solution: store $key in temporary variable:
  19.  
  20.  
  21. class User2{
  22. public $mail = "johan@mail.com";
  23. public $name = "johan";
  24. }
  25.  
  26.  
  27. function return_stuff($key){
  28. $obj = new User2();
  29. $ret = $key;
  30. return $obj->$ret;
  31. }
  32.  
  33. echo return_stuff("name");
  34.  
  35.  
  36.  
  37. // Solution 2: even no need for temp variable, just place $key inside brackets
  38.  
  39. class User3{
  40. public $mail = "johan@mail.com";
  41. public $name = "johan";
  42. }
  43.  
  44.  
  45. function return_stuff($key){
  46. $obj = new User3();
  47. return $obj->{$key};
  48. }
  49.  
  50. echo return_stuff("name");
  51.  
  52.  
  53. ?>
Add Comment
Please, Sign In to add comment