Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7411973
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T06:28:09+00:00 2026-05-29T06:28:09+00:00

I register class methods for actions in my WordPress plugin. When my method gets

  • 0

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>.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T06:28:09+00:00Added an answer on May 29, 2026 at 6:28 am

    You have to instantiate the class first. Something like this:

    $affiliatemarketing = new AffiliateMarketing;
    

    and then do the following:

    add_action('init', array(&$affiliatemarketing, 'initialize'), 1);
    

    Edit: forgot to add, your action in your method should be added like this:

    add_action('woocommerce_before_single_product', array(&$this, "handleAffiliateReferral"));
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a model called Action. It looks like this: class Action < ActiveRecord::Base
I have an interface for Client Registration called IRegistrationService. This contains one method called
I get this error when trying to register a new user. NoMethodError (undefined method
I'm getting this error : undefined local variable or method sessions_path' for #<#:0x2db44c8> when
A Castle Windsor question: Is it possible to register a class that has an
I am trying to register the following class using the fluent interface: public class
I have a class that implements multiple interfaces. I would like to register these
using register_shutdown_function I can register code to execute at the end of a PHP
I have a form that talks to 'process_register.php' to register a user. That file
Controller : <?php class AdminController extends Zend_Controller_Action { public function init() { error_reporting(E_ALL); }

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.