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

发布于 2024-09-06 03:37:06 字数 2573 浏览 6 评论 0原文

我像往常一样创建我的班级。我创建成员变量,设置 setter 和 getter,然后使用 $this 调用成员变量的值。但由于某种原因,现在我得到了致命错误:在不在对象上下文中时使用 $this

这是常规的 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>";
    }

成员变量:

class Site
{   

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

setter 和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

以及我使用 $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):

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. but for some reason now I'm get the fatal error: Using $this when not in object context

Here is the regular php file which should populates the member variables with their value

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:

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):

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

絕版丫頭 2024-09-13 03:37:06

您不能在静态方法中使用 $this。根据定义,静态方法没有关联的实例。

You cannot use $this in a static method. Static methods, by definition, have no associated instance.

咽泪装欢 2024-09-13 03:37:06

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

You cannot use $this in a static function because there is no instantiation to work off of.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文