简体   繁体   English

致命错误:不在对象上下文中时使用$ this

[英]Fatal error: Using $this when not in object context

I got this error, i cant figure whats problem. 我收到此错误,我无法解决问题。 My code is 我的代码是

class Example{
     public function get_lang()
        {
            $jezik = get_option('jezik');
                     switch ($jezik ) {
                         case $jezik == 'rs_RS':
                         $drzava = 'NazivSrb';
                         break;
                         case $jezik == 'ro_RO':
                         $drzava = 'NazivRo';
                         break;
                         case $jezik == 'uk_UK':
                         $drzava = 'NazivRu';
                         break;
                    }
            return $drzava;        
        }

    static public function ExecuteSql($where_criteria=NULL) {
            global $wpdb;

                   $drzva = $this->get_lang();

            $upit = "
                            SELECT ID, Naziv, $drzava, PhoneCode, Kod
                    FROM wp_drzava";
            if ($where_criteria)
                $upit .=" WHERE ". $where_criteria ;
            $upit .= " GROUP BY Kod";
            //echo $upit;
            return $wpdb->get_results($upit);
        }

{

Basically I try to use get_lang method return value, and save it to variable in other method, and pass it to query. 基本上,我尝试使用get_lang方法的返回值,并将其保存到其他方法的变量中,然后将其传递给查询。 But i get an error in this line 但是我在这一行中出错

$drzva = $this->get_lang();

You can't use $this inside static method.. use self keyword if you want to use same class method inside static method 您不能在static方法中使用$this ..如果要在static方法中使用相同的类方法,请使用self关键字

self::get_lang();

EDIT: get_lang() should static method, if you are not using class properties inside method. 编辑:如果您未在方法内部使用类属性,则get_lang()应该为静态方法。

You cannot use $this in a static public function ( $this does not exist in it). 您不能在静态公共函数中使用$this$this在其中不存在)。

To fix this, use: 要解决此问题,请使用:

$example = new Example;
$drzva = $example->get_lang();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM