1

このコードが機能しない理由を教えてもらえますか? 提案されたタスクを実行する最も効率的な方法のように思えますが、Key<>Value を逆にしてもエラーが発生し続ける理由がわかりません。

テキスト文字列/配列内の #tags# を外部クラスから static::variables に置き換えようとしています。

エラー:

Parse error: syntax error, unexpected T_VARIABLE, expecting T_STRING in /home/content/57/10764257/html/marketing/includes/ProcessEmail.class.php on line 5

外部クラス:

class MyClass {
    public static $firstName = "Bob";    // Initally set as "= null", assigned
    public static $lastName = "Smith";   // statically through a call from
}                                        // another PHP file.

メイン PHP ファイル:

// This is the array of find/replace strings

private static $tags = array("#fistName#", MyClass::$firstName,
                             "#lastName#", MyClass::$lastName);

// This jumps trough the above tags, and replaces each tag with
// the static variable from MyClass.class.php

public static function processTags($message) {

    foreach ($tags as $tag => $replace) {
        $message = str_replace($tag, $replace, $message);
    }

}

しかし、私はそのエラーが発生し続けます...?

ありがとうございました!

4

2 に答える 2

3

次のコードを置き換えてみてください。

<?php 
class MyClass {
    private static $tags = array(
        '#firstName#' => 'Bob',
        '#lastName#'  => 'Smith'
    );

    public static function processTags(&$message) {
        foreach (self::$tags as $tag => $replace) {
            $message = str_replace($tag, $replace, $message);
        }
    }
}


$message = 'Hello, my name is #firstName# #lastName#';
MyClass::processTags($message);
echo($message);

結果:

Hello, my name is Bob Smith
于 2013-05-01T12:44:52.733 に答える