php可以调用非静态方法而不首先实例化该类吗?

I register class methods for actions in my Wordpress plugin. When my method gets called by Wordpress, if I try to use the $this variable, php throws an error saying the call to $this variable is illegal outside the context of an object.

How can that be? I thought unless the method is static, you're not supposed to be able to call class methods if the class isn't instantiated! My method isn't static! What is happening?

The source code


Obviously the initialize is called from the main plugin file:

add_action('init', array('AffiliateMarketting', 'initialize'), 1);

My class looks like this:

class AffiliateMarketting 
{
    public function __construct()
    {
        // some initialization code
    }

    public function initialize()
    {
        add_action('woocommerce_before_single_product', array("AffiliateMarketting", "handleAffiliateReferral"));
    }

    public function handleAffiliateReferral($post)
    {
        $this->xxx();  // <---- offending function call
    }

    public function xxx()
    {

    }
}

The received error message is in fact Fatal error: Using $this when not in object context in <filename> on line <linenumber>.

查看全部
douju2012
douju2012
2012/01/31 07:57
  • wordpress
  • php
  • 点赞
  • 收藏
  • 回答
    私信
满意答案
查看全部

4个回复