致命的なエラー:オブジェクトのコンテキストにない場合に$を使用する

StackOverflow https://stackoverflow.com/questions/3086486

  •  28-09-2019
  •  | 
  •  

質問

私はいつものようにクラスを作成しています。メンバー変数を作成し、セッターとゲッターをセットアップし、メンバー変数の値を呼び出すためにこれを使用します。しかし、何らかの理由で今私は取得しています 致命的なエラー:オブジェクトのコンテキストにない場合に$を使用する

これは、メンバー変数にその値に浸透する必要がある通常の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;

セッターとゲッター:

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-> 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):
役に立ちましたか?

解決

使用できません $this 静的方法で。定義上、静的方法には関連するインスタンスがありません。

他のヒント

使用できません $this 動作するインスタンスがないため、静的関数で。

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top