Advertisement
Guest User

Untitled

a guest
Feb 5th, 2016
48
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.17 KB | None | 0 0
  1. <?php
  2.  
  3. // Example usage:
  4. function mytheme_preprocess_node(&$vars) {
  5. $wrapper = new CantTouchThis($vars['node']);
  6. $wrapper->protect('type', true);
  7. $vars['node'] = $wrapper;
  8. }
  9.  
  10.  
  11.  
  12. class CantTouchThis {
  13.  
  14. private $protected = array();
  15. private $object;
  16.  
  17. public function __construct($object) {
  18. $this->object = $object;
  19. }
  20.  
  21. public function protect($property_name, $alert = false) {
  22. $this->protected[$property_name] = $alert;
  23. }
  24.  
  25. public function __set($key, $value) {
  26. if(!isset($this->protected[$key])) {
  27. $this->object->$key = $value;
  28. }
  29. elseif($this->protected[$key]) {
  30. list(, $caller) = debug_backtrace(0, 2) + array(null, null);
  31. if($caller) {
  32. $msg = strtr('Attempt to change property {property} to value "{value}", initiated in function {func} (file {file}, line {line})', array(
  33. '{property}' => (string) $key,
  34. '{value}' => (string) $value,
  35. '{func}' => $caller['function'],
  36. '{file}' => $caller['file'],
  37. '{line}' => $caller['line'],
  38. ));
  39. throw new Exception($msg);
  40. }
  41. }
  42. }
  43.  
  44. public function __get($key) {
  45. return $this->object->$key;
  46. }
  47.  
  48. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement