0

Tplこの関数(template.php)でテンプレートをマウントするクラスがあります

function Set($var, $value){
     $this->$var = $value;
  }

関数を呼び出す php ファイルの例 (form.php):

$t->Set("lbAddress","Address");

タグ付きのテンプレートを含む html ファイル (template.html)

<tr><td>[lbAdress]</td></tr>

HTMLを印刷するには、この関数(template.php)があります-通知はこの関数を指しています

function Show_Temp($ident = ""){
     // create array
     $arr = file($this->file);
     if( $ident == "" ){
        $c = 0; 
        $len = count($arr); 
        while( $c < $len ){
           $temp = str_replace("[", "$" . "this->", $arr[$c]);
           $temp = str_replace("]", "", $temp);
           $temp = addslashes($temp);
           eval("\$x = \"$temp\";");
           echo $x;
           $c++;
        }
     } else {
        $c = 0;
        $len = count($arr);
        $tag = "*=> " . $ident;
        while( $c < $len ){
           if( trim($arr[$c]) == $tag ){
              $c++;
              while( (substr(@$arr[$c], 0 ,3) != "*=>" ) && ($c < $len) ){
                 $temp = str_replace("[", "$" . "this->", $arr[$c]);
                 $temp = str_replace("]", "", $temp);
                 $temp = addslashes($temp);
                 eval("\$x= \"$temp\";"); //this is the line 200
                 echo $x;
                 $c++;
              }
              $c = $len;
           }
           $c++;
        }
     }
  }

テンプレート .html に行があり、php コードに[lbName]行がない場合、エラーが発生します。私が見つけた解決策は、 のような行を追加することですが、PHP で使用しない HTML に 50 個のタグがある場合は、50 個すべてを追加する必要があります。PHP 5 への移行後にエラーが発生しました。誰か助けてもらえますか? ありがとう$t->Set("lbName","Name");PHP Notice: Undefined property: Tpl::$lbName in ../template.php(200) : eval()'d code on line 1$t->Set("lbName","");$t->Set("tag_name","");

4

1 に答える 1

1

おそらく、より良い方法は、動的な評価に依存せずeval(可能な場合は避けるのが一般的に最善です)、必要に応じてオブジェクトに直接格納されている値evalに置き換えることです。[lbName]に置き換えることができる場合は、その場で調べたの値[lbName]$this->lbName置き換えることもできますか?lBName


ただし、元の質問に答えるには:

私の理解が正しければ、次のように値を設定しています。

$t->Set('foo', 'bar');

そして – 効果的に – 次のように取得します。

$t->foo;

その場合は、__getメソッドを実装してプロパティ参照をインターセプトし、値を取得するための独自のロジックを提供できます。例えば:

public function __get($key)
{
    // You can adapt this logic to suit your needs.
    if (isset($this->$key))
    {
        return $this->$key;
    }
    else
    {
        return null;
    }
}

__getこの場合、バッキング ストアとして連想配列を使用し、and を使用し__setてそれにアクセスする方がよいでしょう。例えば:

class Template
{
    private $values = array();

    public function __get($key)
    {
        if (array_key_exists[$key, $this->values])
        {
            return $this->values[$key];
        }
        else
        {
            return null;
        }
    }

    public function __set($key, $value)
    {
        $this->values[$key] = $value;
    }
}
于 2013-01-18T13:24:54.240 に答える