简体   繁体   English

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

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

I'm creating my class like I always do. 我像往常一样创建班级。 I create the member variables, I setup the setter and getter and the I use $this to call the values of the member variables. 创建成员变量,设置setter和getter,然后使用$ this调用成员变量的值。 but for some reason now I'm get the fatal error: Using $this when not in object context 但是由于某种原因,我现在遇到了致命错误:不在对象上下文中使用$ this

Here is the regular php file which should populates the member variables with their value 这是常规的php文件,应使用其值填充成员变量

try
    {
        $add_advert = new Site();
        $add_advert->add_time = mysqli_real_escape_string($link, $_POST['time']);
        $add_advert->add_url = mysqli_real_escape_string($link, $_POST['URL']);
        $add_advert->add_advertisement($_FILES);
        $feedback = "<div class='succes'>Advertisement succesfully added!</div>";
    }
    catch(Exception $e)
    {
        $feedback = "<div class='error'>";
        $feedback .= $e->getMessage();
        $feedback .= "</div>";
    }

the member variables: 成员变量:

class Site
{   

    protected $add_url;
    protected $add_time;
    protected $extend_id;

the setter and getter: 设置者和获取者:

public function __set($p_sProperty, $p_sValue)
{
    switch($p_sProperty)
    {
        case "add_url":
            $this->add_url = $p_sValue;
        break;
        case "add_time":
            $this->add_time = $p_sValue;
        break;
        case "extend_id":
            $this->extend_id = $p_sValue;
        break;
    }
}//end of setter


public function __get($p_sProperty)
{
    $vResult = "";
    switch($p_sProperty)
    {
        case "add_url":
            $vResult = $this->add_url;
        break;
        case "add_time":
            $vResult = $this->add_time;
        break;
        case "extend_id":
            $vResult = $this->extend_id;
        break;
    }
    return $vResult;
}// end of getter

and the function where I use the $this object (it's only a part of the function), it's the $this->add_time that is causing the problem: 以及我在其中使用$ this对象的函数(它只是函数的一部分),是导致问题的$ this-> add_time:

public static function add_advertisement($files)
    {
        global $link, $userid, $useridentify;

        if(!isset($files['image']['tmp_name']) || empty($files['image']['tmp_name'])):
            throw new exception("No image given, enter an image please.");
        endif;

        if($_POST['URL'] == NULL || $_POST['URL'] == "http://"):
            throw new exception("No url given, enter a valid url please.");
        endif;

        if($_POST['time'] == NULL):
            throw new exception("You did not select any option to advertise. Select one of the available advertising options.");
        endif;

        if(!$path = Uploader::upload_file($files, 'images/', 'gif|jpg|jpeg|jpe|png', 300000)):
            throw new exception("Could not upload the image.");
        endif;

        if(self::get_balance() >= $this->add_time):

You cannot use $this in a static method. 您不能在静态方法中使用$this Static methods, by definition, have no associated instance. 根据定义,静态方法没有关联的实例。

您不能在静态函数中使用$this ,因为没有实例可以使用。

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

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