Foros del Web » Programando para Internet » PHP »

Problemas factory.class.php

Estas en el tema de Problemas factory.class.php en el foro de PHP en Foros del Web. HOLA BUENAS NOCHES A TODOS.. RECURRO AQUI PARA SOLICITAR INFORMACION SOBRE EL SIGUIENTE TEMA POR QUE DEBIDO A QUE TENGO QUE CAMBIAR DE VERSION DE ...
  #1 (permalink)  
Antiguo 04/04/2009, 21:09
 
Fecha de Ingreso: octubre-2008
Mensajes: 76
Antigüedad: 15 años, 6 meses
Puntos: 3
Problemas factory.class.php

HOLA BUENAS NOCHES A TODOS.. RECURRO AQUI PARA SOLICITAR INFORMACION SOBRE EL SIGUIENTE TEMA POR QUE DEBIDO A QUE TENGO QUE CAMBIAR DE VERSION DE PHP 4 A PHP 5 Y YA TENGO MI SITIO WEB ARMADO Y ME DA EL SIGUIENTE PROBLEMA LA CLASE FACTORY.CLASS.PHP ESTA CLASE SE ENCARGA DE INSTANCIAR CADA METODO Y AHORA ME TIRA ERROR PARA ESTA VERSION DE PHP POR FAVOR SI ALGUIEN ME PUEDE DAR UNA AYUDA LO AGRADECERIA..
ESTA ES LA CLASE
<?php

class Factory {

var $sufijo = '.class.php';
var $arr_lib_dir = array( '/home/jokerage/public_html/lib/');

function Factory ( $class, $path = null ) {
if ( empty($path) ) {

foreach ( $this->arr_lib_dir as $value ) {
if ( $this->_IsValid($class, $value) ) {
$path = $value;
break;
}
}
if ( empty($path) ) {
die('No se puede encontrar el archivo de la clase en los path especificados');
}
}
if ( $this->_IsValid($class, $path) ) {
if ( !class_exists($class) ) require_once $path.$class.$this->sufijo;
if ( class_exists($class) ) {
$args = '';
$num_args = func_num_args();
if ( $num_args > 2 ) {
$lista_args = func_get_args();
for ($i = 2; $i < $num_args; $i++) {
if ( is_array($lista_args[$i]) ) {
$args .= 'array('.$this->_arr2str($lista_args[$i]).'),';
}
else {
$args .= '"'.$lista_args[$i].'",';
}
}
$args = rtrim($args, ',');
}
unset(this);
$this = eval("return new $class($args);");//EN ESTA LINEA TIRA EL ERROR

}
else {
die('No se puede incluir la clase');
}
}
else {
die('No se puede encontrar el archivo de la clase');
}
}


function _IsValid($class, $path) {

if ( is_file($path.$class.$this->sufijo) ) {
return true;
}
}

function _arr2str($array){
$line = "";
foreach($array as $key => $value){
if(is_array($value)){
$value = "array (". $this->_arr2str($value) . ")";
}
else {
$value = "'".$value."'";
}
if (is_numeric($key)) {
$line .= "," . $key;
}
else {
$line .= ",'" . $key. "'";
}
$line .= "=> ". $value;
}
$line = substr($line, 1);
return $line;
}


}
?>
  #2 (permalink)  
Antiguo 04/04/2009, 21:10
Avatar de Ronruby  
Fecha de Ingreso: julio-2008
Ubicación: 18°30'N, 69°59'W
Mensajes: 4.879
Antigüedad: 15 años, 9 meses
Puntos: 416
Respuesta: Problemas factory.class.php

¿Que error te muestra?
  #3 (permalink)  
Antiguo 04/04/2009, 21:15
Avatar de GatorV
$this->role('moderador');
 
Fecha de Ingreso: mayo-2006
Ubicación: /home/ams/
Mensajes: 38.567
Antigüedad: 18 años
Puntos: 2135
Respuesta: Problemas factory.class.php

Creo el problema es la anterior, haces un unset(this) (te falta el $), por otro lado no es lo mejor asignar directo a $this, mejor regresa una instancia de la clase usando return new $class($params);

Saludos.
  #4 (permalink)  
Antiguo 04/04/2009, 21:20
 
Fecha de Ingreso: octubre-2008
Mensajes: 76
Antigüedad: 15 años, 6 meses
Puntos: 3
Fatal error: Cannot re-assign $this in /home/jokerage/public_html/lib/Factory.class.php on line 37

este es la clase perdon
<?php

class Factory {

var $sufijo = '.class.php';
var $arr_lib_dir = array('C:/Domains/estudioi.es/wwwroot/admin/lib/', '/home/jokerage/public_html/lib/', '/home/identidad/www/clientes/ccmx/cms/lib/', 'c:/www/identidad/lib/', 'c:/www/identidad/estudioi/lib/', '/www/jokerapuestas.com.ar/htdocs/lib/');

function Factory ( $class, $path = null ) {
if ( empty($path) ) {
foreach ( $this->arr_lib_dir as $value ) {
if ( $this->_IsValid($class, $value) ) {
$path = $value;
break;
}
}
if ( empty($path) ) {
die('No se puede encontrar el archivo de la clase en los path especificados');
}
}
if ( $this->_IsValid($class, $path) ) {
if ( !class_exists($class) ) require_once $path.$class.$this->sufijo;
if ( class_exists($class) ) {
$args = '';
$num_args = func_num_args();
if ( $num_args > 2 ) {
$lista_args = func_get_args();
for ($i = 2; $i < $num_args; $i++) {
if ( is_array($lista_args[$i]) ) {
$args .= 'array('.$this->_arr2str($lista_args[$i]).'),';
}
else {
$args .= '"'.$lista_args[$i].'",';
}
}
$args = rtrim($args, ',');
}
$this = eval("return new $class($args);");
}
else {
die('No se puede incluir la clase');
}
}
else {
die('No se puede encontrar el archivo de la clase');
}
}


function _IsValid($class, $path) {
if ( is_file($path.$class.$this->sufijo) ) {
return true;
}
}

function _arr2str($array){
$line = "";
foreach($array as $key => $value){
if(is_array($value)){
$value = "array (". $this->_arr2str($value) . ")";
}
else {
$value = "'".$value."'";
}
if (is_numeric($key)) {
$line .= "," . $key;
}
else {
$line .= ",'" . $key. "'";
}
$line .= "=> ". $value;
}
$line = substr($line, 1);
return $line;
}


}
?>
el anterior era unas pruebas

Última edición por GatorV; 04/04/2009 a las 23:57
  #5 (permalink)  
Antiguo 04/04/2009, 23:58
Avatar de GatorV
$this->role('moderador');
 
Fecha de Ingreso: mayo-2006
Ubicación: /home/ams/
Mensajes: 38.567
Antigüedad: 18 años
Puntos: 2135
Respuesta: Problemas factory.class.php

Ve el mensaje anterior que te deje, no puedes reasignar $this ya que es el objeto, usa mejor otro Factory Pattern para poder regresar el objeto.

Aquí: http://www.phppatterns.com/docs/start puedes ver ejemplos de como implantar patrones.

Saludos.
  #6 (permalink)  
Antiguo 05/04/2009, 08:27
 
Fecha de Ingreso: marzo-2009
Mensajes: 2
Antigüedad: 15 años, 1 mes
Puntos: 0
Problema Clase Factory

Estoy arreglando un sitio, andaba todo bien, pero cuando lo movimos de servidor, me tira tira este error:

Fatal error: Cannot re-assign $this in /home/jokerage/public_html/lib/Factory.class.php on line 37

Yo estoy sospechando, de la versión de php, porque en el servidor anterior estaba corriendo con php4, en este servidor va con php5...

el codigo de la clase que falla es:
Código PHP:
<?php

    
class Factory {

        var 
$sufijo '.class.php';
        var 
$arr_lib_dir = array('C:/Domains/estudioi.es/wwwroot/admin/lib/''/home/jokerage/public_html/lib/''/home/identidad/www/clientes/ccmx/cms/lib/''c:/www/identidad/lib/''c:/www/identidad/estudioi/lib/''/www/jokerapuestas.com.ar/htdocs/lib/');

        function 
Factory $class$path null ) {
            if ( empty(
$path) ) {
                foreach ( 
$this->arr_lib_dir as $value ) {
                    if ( 
$this->_IsValid($class$value) ) {
                        
$path $value;
                        break;
                    }
                }
                if ( empty(
$path) ) {
                    die(
'No se puede encontrar el archivo de la clase en los path especificados');
                }
            }
            if ( 
$this->_IsValid($class$path) ) {
                if ( !
class_exists($class) ) require_once $path.$class.$this->sufijo;
                if ( 
class_exists($class) ) {
                    
$args '';
                    
$num_args func_num_args();
                    if ( 
$num_args ) {
                        
$lista_args func_get_args();
                        for (
$i 2$i $num_args$i++) {
                               if ( 
is_array($lista_args[$i]) ) {
                                
$args .= 'array('.$this->_arr2str($lista_args[$i]).'),';
                               }
                               else {
                                   
$args .= '"'.$lista_args[$i].'",';
                               }
                           }
                           
$args rtrim($args',');
                    }
                    
$this = eval("return new $class($args);");
                }
                else {
                    die(
'No se puede incluir la clase');
                }
            }
            else {
                die(
'No se puede encontrar el archivo de la clase');
            }
        }


        function 
_IsValid($class$path) {
            if ( 
is_file($path.$class.$this->sufijo) ) {
                return 
true;
            }
        }

        function 
_arr2str($array){
            
$line "";
            foreach(
$array as $key => $value){
                if(
is_array($value)){
                    
$value "array ("$this->_arr2str($value) . ")";
                }
                else {
                    
$value "'".$value."'";
                }
                if (
is_numeric($key)) {
                    
$line .= "," $key;
                }
                else {
                    
$line .= ",'" $key"'";
                }
                
$line .= "=> "$value;
            }
            
$line substr($line1);
            return 
$line;
        }


    }
?>
la linea que falla es $this = eval("return new $class($args);");, ya probe poniendo return(eval("return new $class($args);");, pero mismo efecto, probe comentando esta sentencia... pero como es obvio.... no crea instancia de ninguna clase.. entonces factory no serviria de mucho...
  #7 (permalink)  
Antiguo 05/04/2009, 10:25
Avatar de Ronruby  
Fecha de Ingreso: julio-2008
Ubicación: 18°30'N, 69°59'W
Mensajes: 4.879
Antigüedad: 15 años, 9 meses
Puntos: 416
Respuesta: Problema Clase Factory

¿Deja vú?
http://www.forosdelweb.com/f18/probl...ss-php-686555/

Mismo tema, misma duda, ¿diferentes usuarios?
  #8 (permalink)  
Antiguo 05/04/2009, 11:23
Avatar de GatorV
$this->role('moderador');
 
Fecha de Ingreso: mayo-2006
Ubicación: /home/ams/
Mensajes: 38.567
Antigüedad: 18 años
Puntos: 2135
Respuesta: Problema Clase Factory

Temas unidos, es el mismo error.
Atención: Estás leyendo un tema que no tiene actividad desde hace más de 6 MESES, te recomendamos abrir un Nuevo tema en lugar de responder al actual.
Respuesta




La zona horaria es GMT -6. Ahora son las 16:43.