[wp-hackers] PHP 4/5 Classes and WP filters. $this is not working.

Jason Webster jason at intraffic.net
Sat Jun 14 02:42:08 GMT 2008


It should work properly if you pass $this by reference to the array in 
add_filter/add_action.


Aaron Harun wrote:
> In one of my plugins I just noticed that it works great on PHP 5 and
> not on PHP 4. I dind't start using classes until PHP 5, but I've
> narrowed it down to the $this keyword used in filters. Below you will
> find a brief snippet of the scripts involved, please let me know if
> you can see why there would be an issue.
>
> The problems I'm having are weird and are specific to my plugin, but
> they don't cause PHP errors and go away if I change the filters from
> $this to the actual class name. It seems to me that $this is referring
> to two different things. One in the contents add_filter and one in the
> remove_filter. I tried moving everything inside the first init()
> function outside, but it didn't change anything.
>
> My guess is that it has to do with the way the class is being called,
> but I'm not sure what part is causing it.
>
> Thanks,
>
> Aaron
>
> p.s. If anyone is interested, the plugin causing the trouble is the
> latest SVN version of
> http://wordpress.org/extend/plugins/ajaxd-wordpress/. The problem is
> occurring when inline posts are being loaded with AJAX.
>
> p.p.s. I can explain the logic behind what is going on if needed or wanted.
>
> function init() {
> 	include_once('aclassfile.php');
> 	include_once('anotherclassfile.php');
>
> 	if(count($classes) > 0){ // $classes is just an array of class names.
> 		foreach($classes as $class){
> 			$$class = null;
> 			global $$class;
> 			$$class = new $class;
> 		}
> 		reset($awp_init);
> 		foreach($classes as $class){
> 			$$class->init();
> 		}
> 	}
> }
>
> 	init();
> 	do_action('awp_ajax_type_post');
>
>
> aclassfile.php
> _____________________
>
> $classes[] = 'AWP_inlineposts';
>
> class AWP_inlineposts {
>
> 	function init(){
> 		$this->awp_live();
> 		add_action('awp_ajax_type_post', 'AJAX');
> 	}
>
> 	function awp_live(){
> 		add_filter('the_content', array($this,'afunction'),-10,1);
> 	}
>
> 	function AJAX($content){
>
> 		remove_filter('the_content', array($this,'afunction'),-10,1);
>
> 		$response[] = apply_filters('the_content',$content);
> 	}
> }
> _______________________________________________
> wp-hackers mailing list
> wp-hackers at lists.automattic.com
> http://lists.automattic.com/mailman/listinfo/wp-hackers
>   



More information about the wp-hackers mailing list