Home  >  Q&A  >  body text

Call variables outside plugin files and inside function files

I use the YITH WooCommerce comparison plugin and I will put the file containing the code relevant to my question:

https://file.io/StHr7KBJBdxF

$current_cat variable has been set equal to: public$current_cat=array() $this->current_cat has been used in some parts of the code, how to call $this-> outside the file and in the function file ;current_cat?

class YITH_Woocompare_Frontend_Premium extends YITH_Woocompare_Frontend {

    …

    /**
     * The list of current cat inside the comparison table
     *
     * @since 1.0.0
     * @var array
     */
    public $current_cat = array();

For example: $this->products_list; is used, if I want to call it outside the file and in the function file, it is like this: $products_list=isset($_COOKIE[get_COOKIE_name()])? json_decode(wp_unslash($_COOKIE[get_COOKIE_name()]): array();

I hope you understand what I mean. If this is a beginner question, please don't diss because not everyone is as professional as you. Furthermore, I spent a lot of time solving this problem. I'm not trying to ask a quick question here to find a quick answer.

P粉798343415P粉798343415430 days ago349

reply all(1)I'll reply

  • P粉262113569

    P粉2621135692023-07-18 09:50:22

    您的current_cat是由您的插件定义的类的公共属性。

    在该类的函数中,代码可以使用$this->current_cat来访问该属性。为什么?在该类的代码中,$this是指向该类当前实例的引用。->对象操作符告诉php在$this内查找。

    在该类的代码之外,您可以通过以下方式访问公共属性。

    $my_object = new PluginClass();
    $cat = $my_object->current_cat;
    您正在尝试执行示例的第二行中显示的操作。要执行此操作,您需要访问第一行返回的new PluginClass()变量。不同的插件有不同的方法来使这种类型的信息可用于主题代码(functions.php)。必须查看插件的代码以确定如何执行此操作,或查看文档,或咨询插件的支持论坛。

    恕我直言,解释php类的工作原理对于Stack Overflow的答案来说太复杂了。解释WordPress、插件和主题代码的交互方式也是如此。我希望这个答案能为您指明一个有用的方向。


    reply
    0
  • Cancelreply