yumichan
yumichan

Reputation: 11

PHP Notice: Undefined property: Tpl::$lbName

I have a class Tpl to mount template with this function (template.php)

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

A php file that call the function, example (form.php):

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

And a html file with the template with tags (template.html)

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

To print the html I have this function (template.php) - the notice points to this function

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++;
        }
     }
  }

If the template .html have a line [lbName] and I don't have the line $t->Set("lbName","Name"); at the php code, I receive the error PHP Notice: Undefined property: Tpl::$lbName in ../template.php(200) : eval()'d code on line 1. The solution that I found is add lines like $t->Set("lbName","");, but if I have 50 tags in HTML that I don't use in PHP, I have to add all 50 $t->Set("tag_name","");. The error occurred after migrate to the PHP 5. Can someone help me? Thanks

Upvotes: 0

Views: 665

Answers (1)

Will Vousden
Will Vousden

Reputation: 33388

Perhaps a better way still would be not to rely on dynamic evaluation through eval (it's generally best to avoid eval where possible), but to replace [lbName] with the value stored in the object directly as and when needed. If you can replace [lbName] with $this->lbName, surely you can also replace it with the value of lBName that you've looked up on-the-fly?


To answer your original question, however:

If I understand correctly, you're setting the values like this:

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

And – effectively – getting them like this:

$t->foo;

If so, you could implement a __get method to intercept the property references and provide your own logic for retrieving the value; e.g.:

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

In this case, you'd probably be better off using an associative array as the backing store, and then using __get and __set to access it; e.g.:

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;
    }
}

Upvotes: 1

Related Questions