Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Okay, </p> <p>I am not 100% exactly what you want to do, but I have an idea.</p> <p>Maybe you mean something like this:</p> <pre><code>class View { public $hooks = array('getsomeVar'); public $hooks_functions = array(); public $attributes = array(); public function __set($k,$v) { $this-&gt;attributes[$k] = $v; } public function __get($k) { if (isset($this-&gt;attributes[$k])){ $hooks = $this-&gt;get_functions_by_hook('get' . $k); if (!empty($hooks)){ foreach ($hooks as $klass=&gt;$methods) { if (class_exists($klass)){ $class = new $klass(); foreach ($methods as $method) { if (method_exists($class,$method)){ $this-&gt;attributes[$k] = $class-&gt;$method($this-&gt;attributes[$k]); } } } } } return $this-&gt;attributes[$k]; } else { throw new Exception($k . " is not a view variable"); } } public function register_filter($name,$class,$method) { if (in_array($name,$this-&gt;hooks)){ $this-&gt;hooks_functions[$name][$class][] = $method; } else { throw new Exception($name . ' is not a valid hook'); } } public function get_functions_by_hook($name) { if (array_key_exists($name,$this-&gt;hooks_functions)){ return $this-&gt;hooks_functions[$name]; } return array(); } } class MyPlugin { public function fix_string($str) { return str_replace("ruby",'php',$str); } } $v = new View(); $v-&gt;someVar = 'ruby is great'; $v-&gt;register_filter('getsomeVar','MyPlugin','fix_string'); echo $v-&gt;someVar; </code></pre> <p>or you can use this method, which is more event like. Again sample code, but you should be able to cannabalize it.</p> <pre><code>class EventDispatcher { public static $listeners = array(); public static function registerListener(&amp;$instance) { if (!in_array($isntance,self::$listeners)){ array_push(self::$listeners,$instance); } } public static function dispatchEvent($name,&amp;$value) { foreach (self::$listeners as $listener) { if (method_exists($listener,'interests')){ $funcs = $listener-&gt;interests(); if (array_key_exists($name,$funcs)){ foreach ($funcs as $f) { $value = $listener-&gt;$f($value); } } } } } } class Plugin { public static function registerPlugin($class_name) { if (class_exists($class_name)){ EventDispatcher::registerListener(new $class_name()); } } } class Model { public function __construct() { EventDispatcher::registerListener($this); } public function special($value) { echo "I got called too!\n\n"; return $value; } public function interests() { return array( "getsomeVar" =&gt; "special", ); } } class View { public $attributes = array(); public function __set($k,$v) { $this-&gt;attributes[$k] = $v; } public function __get($k) { if (isset($this-&gt;attributes[$k])){ EventDispatcher::dispatchEvent('get' . $k,$this-&gt;attributes[$k]); return $this-&gt;attributes[$k]; } else { throw new Exception($k . " is not a view variable"); } } } class MyPlugin { public function fix_string($str) { return str_replace("ruby",'php',$str); } public function interests() { return array( "getsomeVar" =&gt; "fix_string", ); } } Plugin::registerPlugin('MyPlugin'); $model = new Model(); $v = new View(); $v-&gt;someVar = 'ruby is great'; echo $v-&gt;someVar; </code></pre> <p>This is just some sample code, I don't do it this way at all, but it seems like it may be what you are talking about.</p> <p>Cheers, Jason</p> <p>Addendum: </p> <p>Most of this stuff is about the WP codebase.</p> <p>WP accesses variables set in the global scope that are modified like so:</p> <pre><code>function add_filter($tag, $function_to_add, $priority = 10, $accepted_args = 1) { global $wp_filter, $merged_filters; $idx = _wp_filter_build_unique_id($tag, $function_to_add, $priority); $wp_filter[$tag][$priority][$idx] = array('function' =&gt; $function_to_add, 'accepted_args' =&gt; $accepted_args); unset( $merged_filters[ $tag ] ); return true; } </code></pre> <p>It has some other functions like, has_filter etc...</p> <pre><code>function apply_filters($tag, $value) { global $wp_filter, $merged_filters, $wp_current_filter; $args = array(); $wp_current_filter[] = $tag; // Do 'all' actions first if ( isset($wp_filter['all']) ) { $args = func_get_args(); _wp_call_all_hook($args); } if ( !isset($wp_filter[$tag]) ) { array_pop($wp_current_filter); return $value; } // Sort if ( !isset( $merged_filters[ $tag ] ) ) { ksort($wp_filter[$tag]); $merged_filters[ $tag ] = true; } reset( $wp_filter[ $tag ] ); if ( empty($args) ) $args = func_get_args(); do { foreach( (array) current($wp_filter[$tag]) as $the_ ) if ( !is_null($the_['function']) ){ $args[1] = $value; $value = call_user_func_array($the_['function'], array_slice($args, 1, (int) $the_['accepted_args'])); } } while ( next($wp_filter[$tag]) !== false ); array_pop( $wp_current_filter ); return $value; } </code></pre> <p>This is not an event driven system, it's a method lookup table which is just a giant hash that looks up the user defined functions to call.</p> <p>apply_filters, and all plugin like functions are called procedurally as the code is rendered, here is an example</p> <pre><code> if ( $prefixed ) { $value = apply_filters("pre_$field", $value); $value = apply_filters("${field_no_prefix}_save_pre", $value); } else { $value = apply_filters("pre_post_$field", $value); $value = apply_filters("${field}_pre", $value); } </code></pre> <p>Or for actions, in an actual template view like so:</p> <pre><code>&lt;p class="submit"&gt;&lt;input type="submit" class="button" name="submit" value="&lt;?php esc_attr_e('Add Category'); ?&gt;" /&gt;&lt;/p&gt; &lt;?php do_action('edit_link_category_form', $category); ?&gt; &lt;/form&gt; </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload