-1

「オブジェクト コンテキストでないときに $this を使用しています」というエラーが表示され続けます

インスタンス化。

$axre = new Axre();

if (!filter_var($this->email, FILTER_VALIDATE_EMAIL)){
    echo "yes";
} else {
    echo"no";
}

オブジェクト コンテキストを取得するにはどうすればよいですか?

$email で保護されています。フォーム上にあるため、保護する必要があります。

// このフレームワークのほとんどのオブジェクトは、コンストラクターを呼び出すことによって設定されますが、 // これにはさまざまなエントリ ポイントがあります。

class Axre extends Base {


protected $email;
protected $user_name;
protected $temp_token;
protected $sign_in_token;

protected $UserShoppingList;

function __construct($email = null) {

    if (strpos($email, '@') === false) {
        $this->sign_in_token = $email;
    } else {
        $this->email = $email;
    }
}
4

2 に答える 2

0
class Axre extends Base {


protected $email;
protected $user_name;
protected $temp_token;
protected $sign_in_token;

protected $UserShoppingList;

function __construct($email = null) {

    if (strpos($email, '@') === false) {
        $this->sign_in_token = $email;
    } else {
        $this->email = $email;
    }
}

function isValidEmail() {
    if (filter_var($this->email, FILTER_VALIDATE_EMAIL)) {
        return true;
    }
    return false;
}

}

これを使用するには、クラスをインスタンス化し、isValidEmail メソッドを呼び出して true/false の結果を取得します。

$test = new Axre($email);
$validEmail = $test->isValidEmail();

クラスの外で $this を使用して、それが機能することを期待することはできません。

于 2013-11-12T20:28:09.947 に答える
0

$this 変数は、クラスの現在のインスタンスへの特別な参照です。意味のないクラスの外で使用しています。

PHPの例から

class Vegetable {
   var $edible;
   var $color;

   function Vegetable($edible, $color="green") 
   {
       $this->edible = $edible; // $this refers to the instance of vegatable
       $this->color = $color;   // ditto
   }
}

$this->variable; //This does not make sense because it's not inside an instance method
于 2013-11-12T19:57:10.523 に答える