Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- // Example usage:
- function mytheme_preprocess_node(&$vars) {
- $wrapper = new CantTouchThis($vars['node']);
- $wrapper->protect('type', true);
- $vars['node'] = $wrapper;
- }
- class CantTouchThis {
- private $protected = array();
- private $object;
- public function __construct($object) {
- $this->object = $object;
- }
- public function protect($property_name, $alert = false) {
- $this->protected[$property_name] = $alert;
- }
- public function __set($key, $value) {
- if(!isset($this->protected[$key])) {
- $this->object->$key = $value;
- }
- elseif($this->protected[$key]) {
- list(, $caller) = debug_backtrace(0, 2) + array(null, null);
- if($caller) {
- $msg = strtr('Attempt to change property {property} to value "{value}", initiated in function {func} (file {file}, line {line})', array(
- '{property}' => (string) $key,
- '{value}' => (string) $value,
- '{func}' => $caller['function'],
- '{file}' => $caller['file'],
- '{line}' => $caller['line'],
- ));
- throw new Exception($msg);
- }
- }
- }
- public function __get($key) {
- return $this->object->$key;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement