code_junkie

Change the class of an object at runtime

Nov 13th, 2011
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.14 KB | None | 0 0
  1. // existing code --------------------
  2. class JParameter {
  3. function render() {
  4. // return HTML with tables
  5. }
  6. // of course, there's a lot more functions here
  7. }
  8.  
  9. // my magical class -----------------
  10. class MyParameter extends JParameter {
  11. function render() {
  12. // return HTML which doesn't suck
  13. }
  14. }
  15.  
  16. // my code --------------------------
  17. $this->params->render(); // returns tables
  18. $this->params = (MyParameter) $this->params; // miracle occurs here?
  19. $this->params->render(); // returns nice html
  20.  
  21. class MyJParameter {
  22. private $jparm;
  23. function __construct( JParameter $jparm ) {
  24. $this->jparm = $jparm;
  25. }
  26. function render() {
  27. /* your code here */
  28. }
  29. function __get( $var ) {
  30. if( isset( $this->$jparm->$var ) {
  31. return $this->$jparm->$var;
  32. }
  33. return false;
  34. }
  35. function __set( $var, $val ) {
  36. /* similar to __get */
  37. }
  38. function __call( $method, $arguments ) {
  39. if( method_exists( $this->jparm, $method ) {
  40. return call_user_func_array( array( $this->jparm, $method ), $arguments );
  41. }
  42. return false;
  43. }
  44. }
Add Comment
Please, Sign In to add comment