-1

以下のコードを実行すると、致命的なエラー: Using $this when not in object context が発生します。メールを別のアカウントに転送するスクリプトを書いています。私を助けてください。私はphpの初心者です。エラーはif (is_resource($this->connection))行を指しています。しかし、私が知る限り、そこでは問題ありません。

$hostname = '{imap.xyz.com:993/imap/ssl}INBOX';
$username = 'aaaaa@xyz.com';
$password = 'xxxxxxxxx';

$connection = imap_open($hostname,$username,$password) or die('Cannot connect to Tiriyo: ' . imap_last_error());

function Message_Parse($id)
{
    if (is_resource($this->connection))
    {
        $result = array
        (
            'text' => null,
            'html' => null,
            'attachments' => array(),
        );
    $structure = imap_fetchstructure($this->connection, $id, FT_UID);

    if (array_key_exists('parts', $structure))
    {
        foreach ($structure->parts as $key => $part)
        {
            if (($part->type >= 2) || (($part->ifdisposition == 1) && ($part->disposition == 'ATTACHMENT')))
            {
                $filename = null;

                if ($part->ifparameters == 1)
                {
                    $total_parameters = count($part->parameters);

                    for ($i = 0; $i < $total_parameters; $i++)
                    {
                        if (($part->parameters[$i]->attribute == 'NAME') || ($part->parameters[$i]->attribute == 'FILENAME'))
                        {
                            $filename = $part->parameters[$i]->value;

                            break;
                        }
                    }

                    if (is_null($filename))
                    {
                        if ($part->ifdparameters == 1)
                        {
                            $total_dparameters = count($part->dparameters);

                            for ($i = 0; $i < $total_dparameters; $i++)
                            {
                                if (($part->dparameters[$i]->attribute == 'NAME') || ($part->dparameters[$i]->attribute == 'FILENAME'))
                                {
                                    $filename = $part->dparameters[$i]->value;

                                    break;
                                }
                            }
                        }
                    }
                }

                $result['attachments'][] = array
                (
                    'filename' => $filename,
                    'content' => str_replace(array("\r", "\n"), '', trim(imap_fetchbody($this->connection, $id, ($key + 1), FT_UID))),
                );
            }

            else
            {
                if ($part->subtype == 'PLAIN')
                {
                    $result['text'] = imap_fetchbody($this->connection, $id, ($key + 1), FT_UID);
                }

                else if ($part->subtype == 'HTML')
                {
                    $result['html'] = imap_fetchbody($this->connection, $id, ($key + 1), FT_UID);
                }

                else
                {
                    foreach ($part->parts as $alternative_key => $alternative_part)
                    {
                        if ($alternative_part->subtype == 'PLAIN')
                        {
                            echo '<h2>' . $alternative_part->subtype . ' ' . $alternative_part->encoding . '</h2>';

                            $result['text'] = imap_fetchbody($this->connection, $id, ($key + 1) . '.' . ($alternative_key + 1), FT_UID);
                        }

                        else if ($alternative_part->subtype == 'HTML')
                        {
                            echo '<h2>' . $alternative_part->subtype . ' ' . $alternative_part->encoding . '</h2>';

                            $result['html'] = imap_fetchbody($this->connection, $id, ($key + 1) . '.' . ($alternative_key + 1), FT_UID);
                        }
                    }
                }
            }
        }
    }

    else
    {
        $result['text'] = imap_body($this->connection, $id, FT_UID);
    }

    $result['text'] = imap_qprint($result['text']);
    $result['html'] = imap_qprint(imap_8bit($result['html']));

    return $result;
        }

return false;
}
$to      = 'aaaa@gmail.com';
$subject = 'the subject';
$message = 'test';

$id=1;
Message_Parse($id);
$headers = 'From: aaa@xyz.com' . "\r\n" .
    'Reply-To: aaa@xyz.com' . "\r\n" .
    'X-Mailer: PHP/' . phpversion();
$mail = mail($to, $subject, $result, $headers);
if($mail){
echo "YES";
} else{
echo "NO";
}
4

3 に答える 3

0

他のすべての人が言ったように、あなたはオブジェクトの中にいないので、$thisを使用することはできません。

ただし、関数の内部にいるため、アクセス可能にしない限り、この関数の外部の変数にアクセスすることもできません。

を使用した解決策はglobal機能しますが、いつかグローバル変数の名前を変更する必要があると判断した場合は、地獄に落ち着きます$connection

より良い解決策は、それを2番目のパラメーターとして関数に渡すことです。

function Message_Parse($id)になる必要がありますfunction Message_Parse($id, $connection)

if (is_resource($this->connection))になる必要がありますif (is_resource($connection))

$id=1; Message_Parse($id);になる必要があります$id=1; Message_Parse($id, $connection);

終わり。

于 2012-10-04T18:29:32.287 に答える
0

$thisクラスの一部であるメソッド内にのみ存在します。$this関数や、クラスの一部ではない場所には存在しません。詳細については、基本的な OOPに関する PHP マニュアル ページを参照してください。

代わりにグローバル変数を参照する場合$connectionは、関数の先頭を次のように変更します。

function Message_Parse($id)
{
    global $connection;
    if (is_resource($connection))
    {

ただし、グローバル変数を使用することは、どの言語でもプログラミングの習慣として悪いことに注意してください。

于 2012-10-04T18:18:12.770 に答える
0

に変更$this->connectionするだけ$connectionです。 $thisクラスにいる場合にのみ適用されます。global $connection;また、関数の先頭で呼び出す必要がある場合もあります。パラメータに渡されない変数は、このキーワードを呼び出す必要があると思います。

「グローバル キーワード」については、http: //php.net/manual/en/language.variables.scope.phpを参照してください。

于 2012-10-04T18:18:18.183 に答える