0

I have the following code in 7.4 it worked for me now in 8.1 it doesn't work for me... I corrected it by replacing foreach but I still have errors of

Undefined property: Plantilla::$mihtml

help please I hope you can help me I don't know how to solve this problem

<?php
    class Plantilla{
    function Plantilla($template_file){
        $this->tpl_file =$template_file ;
    }
    function asigna_variables($vars){
        $this->vars= (empty($this->vars)) ? $vars : $this->vars . $vars;
    }
    function muestra(){
     
            $this->template_file = $this->tpl_file;
         
            $this->mihtml = $this->template_file;
            $this->mihtml = str_replace ("'", "\'", $this->mihtml);
            $this->mihtml = preg_replace('#\{([a-z0-9\-_]*?)\}#is', "' . $\\1 . '", $this->mihtml);
            reset ($this->vars);
 
                foreach($this->vars as $key => $val) {

                $$key = $val;
            }
            eval("\$this->mihtml = '$this->mihtml';");
            reset ($this->vars);
            foreach($this->vars as $key => $val)
            {
                unset($$key);
            }
            $this->mihtml=str_replace ("\'", "'", $this->mihtml);
            return $this->mihtml;
            //AQUI SI QUEREMOS USARDEFRENTE EN  PARA MOSTTRAR SE DEBE CAMBIAR A ''ECHO'
         
    }
}



$COD_RANDOM ='1234567899999999999999999999999999999999999';


    $Contenido=new Plantilla('el codigo es {CODIGO_RANDOM}');//al Pasar como parametro Prueba, asumimos que en la carpeta   plantillas existe un archivo de nombre Prueba.tpl
    $Contenido->asigna_variables(array(
                    "CODIGO_RANDOM" => $COD_RANDOM  //codigo generado
                    ));
    #$ContenidoString = $Contenido->muestra();//$ContenidoString contiene nuestra plantilla, ya con las variables asignadas, fácil no?
 
      $mensajebody=$Contenido->muestra();
 
  echo  $mensajebody;
?> ```


espero me puedan ayudar por favor
Yhon
  • 1

1 Answers1

-1

You are giving $template when you create an $Contenido object.

new Plantilla('el codigo es {CODIGO_RANDOM}');

You mean it to be set as local property of class. But the construction function in PHP calls not by class name, but with special name __construct()

The code of class should start with the following

class Plantilla
{
    function __construct($template_file)      //<-- this function name changes
    {
        $this->tpl_file = $template_file;
    }
}

Please, follow this manual: https://www.php.net/manual/en/language.oop5.decon.php