没有$ this的情况下如何处理类变量? [英] How to handle class variable without $this?

查看:58
本文介绍了没有$ this的情况下如何处理类变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在制作具有OOP概念的WordPress插件,并且遇到了一些有线问题.

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

首先,我有一个main-plugin.php文件,其中有一个类似这样的类

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

现在我有另一个php文件,其中包含另一个包含在main-plugin.php文件中的类

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

假设新文件为something.php

我在这里

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

现在我不知道为什么$this在else类中不起作用.另外,由于这些变量不是静态变量,所以我真的不能self::$b.因此,我希望其他任何人都可以告诉我一种无需使用$this即可访问类内部非静态变量的方法.

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.

推荐答案

我认为,您必须在

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' ) );

这篇关于没有$ this的情况下如何处理类变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆