iSaumya iSaumya - 1 month ago 9
PHP Question

How to handle class variable without $this?

I'm making a WordPress plugin with OOP concept and I'm facing some wired issue.

First I have a

main-plugin.php
file where I have a class like this

include_once plugin_dir_path( __FILE__ ) . 'something.php';

class something {
//some var
public $a;
private function __construct() { {
//all of my wordpress hook calls are here
}

public function foo() {
$this->a = 55; // this is working fine and not giving any error
}
}


Now I have another php file which holds another class which I included in the
main-plugin.php
file

let's say the new file is
something.php


and here I have

class some {
//some variables
public $b, $c;

// now this this class when I'm doming
public function bar() {
$this->b =1; // this is giving me error saying this is not an object.
}
}


Now I have no idea why
$this
is not working inside the else class. Also As these variables are not static variables, I really can't do
self::$b
. So, I was hoping if anyone else can tell me a way to access non static variables inside the class without using
$this
.

Real Problem Link:



In case the above example is not exactly clear to you, here is the link of my actual project in github where you can check the whole code to get the full pucture.

Github Repo Link: https://github.com/isaumya/adsense-invalid-click-protector

Now here the main plugin file is https://github.com/isaumya/adsense-invalid-click-protector/blob/master/adsense-invalid-click-protector.php where I'm including my other PHP files which handels seperate things.

Now as
adsense-invalid-click-protector.php
is the main plugin file, all the
action
&
filter
call gets handelled by the constructor of the class on this file.

Now within
adsense-invalid-click-protector.php
file's class
AICP
$this->
is working fine.

But on my other files like https://github.com/isaumya/adsense-invalid-click-protector/blob/master/inc/admin_setup.php where I have a class called
AICP_ADMIN
, when I'm trying access the
AICP_ADMIN
class variables by using
$this->
within the
AICP_ADMIN
class, it is throwing exception like this:

[06-Nov-2016 17:56:48 UTC] PHP Fatal error: Uncaught Error: Using $this when not in object context in /home/example/test.example.org/wp-content/plugins/adsense-invalid-click-protector/inc/admin_setup.php:184
Stack trace:
#0 /home/example/test.example.org/wp-admin/includes/template.php(1340): AICP_ADMIN::click_limit_field(Array)
#1 /home/example/test.example.org/wp-admin/includes/template.php(1299): do_settings_fields('aicp_settings', 'aicp_section')
#2 /home/example/test.example.org/wp-content/plugins/adsense-invalid-click-protector/inc/admin_setup.php(101): do_settings_sections('aicp_settings')
#3 /home/example/test.example.org/wp-includes/plugin.php(524): AICP_ADMIN::admin_settings_page('')
#4 /home/example/test.example.org/wp-admin/admin.php(222): do_action('toplevel_page_a...')
#5 {main}
thrown in /home/example/test.example.org/wp-content/plugins/adsense-invalid-click-protector/inc/admin_setup.php on line 184


So, I cannot use
$this->
approach inside the
AICP_ADMIN
class to access my variables and methods. So, I was wondering is there is any other way of handelling this without manking my variables a
static
variable and accessing them like
self::$a
.

Thanks in advance.

Answer

I think, you must have instance of AICP_ADMIN in https://github.com/isaumya/adsense-invalid-click-protector/blob/master/adsense-invalid-click-protector.php#L91 instead of string. So try something like

$aicpAdmin = new AICP_ADMIN;
add_action( 'admin_enqueue_scripts', array( $aicpAdmin, 'admin_scripts' ) );