Un objeto dentro de un problema objeto

  • Bogey
  • Bogey
  • Genius
  • Avatar de Usuario
  • Registrado: Jul 14, 2005
  • Mensajes: 8212
  • Loc: USA
  • Status: Offline

Nota Mayo 14th, 2011, 1:26 am

Tengo el siguiente código
PHP Código: [ Select ]
class template {
    function template()
    {
        $this->time = new time();
    }
}
 
class time {
    function format_time($a, $b)
    {
        return date($a, $b);
    }
}
  1. class template {
  2.     function template()
  3.     {
  4.         $this->time = new time();
  5.     }
  6. }
  7.  
  8. class time {
  9.     function format_time($a, $b)
  10.     {
  11.         return date($a, $b);
  12.     }
  13. }

y luego en entonces en el archivo de plantilla, trato de acceder a ella como
PHP Código: [ Select ]
<?=$tpl->time->format_time($c, $d);?>

Eso es echarme el siguiente error:
Quote:
Fatal error: Llamada a un format_time función miembro () en un no-objeto en C: \ wamp \ www \ CMS \ style \ default \ viewpost.php en la línea 24

¿Cómo puedo solucionar ese error?

Cuando hago print_r ($ this-> tiempo) o var_dump ($ this-> tiempo) en la clase de plantilla que me dice su objeto.
var_dump
Quote:
objeto (tiempo) #15 (0) {}

print_r
Quote:
Objeto del tiempo ()
"Bring forth therefore fruits meet for repentance:" Matthew 3:8
  • Anonymous
  • Bot
  • No Avatar
  • Registrado: 25 Feb 2008
  • Mensajes: ?
  • Loc: Ozzuland
  • Status: Online

Nota Mayo 14th, 2011, 1:26 am

  • WritingBadCode
  • Graduate
  • Graduate
  • Avatar de Usuario
  • Registrado: Abr 28, 2011
  • Mensajes: 214
  • Loc: Sweden
  • Status: Offline

Nota Mayo 14th, 2011, 1:53 am

Yo no sé acerca de lo siguiente:

format_time $ tpl-> tiempo-> ($ c, $ d);

Prueba:

$ tpl = time () -> format_time ($ c, $ d);

O:

$ Tpl = nuevo tiempo ();
format_time $ tpl-> ($ c, $ d);
  • Bogey
  • Bogey
  • Genius
  • Avatar de Usuario
  • Registrado: Jul 14, 2005
  • Mensajes: 8212
  • Loc: USA
  • Status: Offline

Nota Mayo 14th, 2011, 2:02 am

La cosa con $ tpl es que tpl $ tiene la clase de plantilla. No se puede tener se establece en un objeto diferente.

Razón de que esto me desconcierta porque siempre trabajó para mí...tal vez me olvidé de cómo hacer eso.
"Bring forth therefore fruits meet for repentance:" Matthew 3:8
  • Bogey
  • Bogey
  • Genius
  • Avatar de Usuario
  • Registrado: Jul 14, 2005
  • Mensajes: 8212
  • Loc: USA
  • Status: Offline

Nota Mayo 14th, 2011, 2:20 am

Lo siento, creo que no debe ser tan secreto que lo que mis clases contienen :lol:

template.php
PHP Código: [ Select ]
<?php
/*
 * template.php ~ The template class for the CMS
 */
 
// Making sure there are no unauthorized use of this page
if(!defined('IN_CMS'))
{
    die(NOT_IN_CMS);
}
 
class template {
 
    // Initiating the public class variables
   public $templateDir;    // The Locationg of the Template Files
   public $css;            // The Location of the CSS Files
   public $meta;           // The meta information
   public $time;           // The time class
   
   /*
    * function template( void )
    *
    * Constructor function for the class that sets the needed variables
    */
   
   function template()
   {
       global $root, $sess;
       
       // If we are logged in
       $this->logged_in = $sess->logged();
       
        $this->iconUrl = "{$root}images/icons";
       
        // Setting some global template variables
        $this->time = new time();
    }
   
   /*
    * public function display( $file1, $file2, $file2 ... )
    *
    * The function that displays the page
    */
   
   public function display($file)
   {
        global $root;
       
      // Allowing the class to be accessible throughout the design
      $tpl = $this;
     
      // The CSS inclusion code
      $this->css = "{$root}style/{$this->templateDir}/style";
     
        // Looping through each file we need to include and include it
        foreach(func_get_args() as $file)
        {
            // Checking if the file is valid and if it exists
            if(!strpos('.', $file) && !strpos('/', $file) && file_exists("{$root}style/{$this->templateDir}/{$file}.php"))
            {
                // Including the actual site design
                include_once("{$root}style/{$this->templateDir}/{$file}.php");
            }
        }
    }
}
  1. <?php
  2. /*
  3.  * template.php ~ The template class for the CMS
  4.  */
  5.  
  6. // Making sure there are no unauthorized use of this page
  7. if(!defined('IN_CMS'))
  8. {
  9.     die(NOT_IN_CMS);
  10. }
  11.  
  12. class template {
  13.  
  14.     // Initiating the public class variables
  15.    public $templateDir;    // The Locationg of the Template Files
  16.    public $css;            // The Location of the CSS Files
  17.    public $meta;           // The meta information
  18.    public $time;           // The time class
  19.    
  20.    /*
  21.     * function template( void )
  22.     *
  23.     * Constructor function for the class that sets the needed variables
  24.     */
  25.    
  26.    function template()
  27.    {
  28.        global $root, $sess;
  29.        
  30.        // If we are logged in
  31.        $this->logged_in = $sess->logged();
  32.        
  33.         $this->iconUrl = "{$root}images/icons";
  34.        
  35.         // Setting some global template variables
  36.         $this->time = new time();
  37.     }
  38.    
  39.    /*
  40.     * public function display( $file1, $file2, $file2 ... )
  41.     *
  42.     * The function that displays the page
  43.     */
  44.    
  45.    public function display($file)
  46.    {
  47.         global $root;
  48.        
  49.       // Allowing the class to be accessible throughout the design
  50.       $tpl = $this;
  51.      
  52.       // The CSS inclusion code
  53.       $this->css = "{$root}style/{$this->templateDir}/style";
  54.      
  55.         // Looping through each file we need to include and include it
  56.         foreach(func_get_args() as $file)
  57.         {
  58.             // Checking if the file is valid and if it exists
  59.             if(!strpos('.', $file) && !strpos('/', $file) && file_exists("{$root}style/{$this->templateDir}/{$file}.php"))
  60.             {
  61.                 // Including the actual site design
  62.                 include_once("{$root}style/{$this->templateDir}/{$file}.php");
  63.             }
  64.         }
  65.     }
  66. }

time.php
PHP Código: [ Select ]
class time {
 
    function time()
    {
       
    }
 
    public function format_time($tmsp, $userID)
    {
        // Returning the formatted date/time
        return date('D M j, Y g:i a', $tmsp);
    }
 
}
  1. class time {
  2.  
  3.     function time()
  4.     {
  5.        
  6.     }
  7.  
  8.     public function format_time($tmsp, $userID)
  9.     {
  10.         // Returning the formatted date/time
  11.         return date('D M j, Y g:i a', $tmsp);
  12.     }
  13.  
  14. }

Mi uso de
PHP Código: [ Select ]
<?=$tpl->time->format_time($column['threadPostDate'], $column['userID']);?>
"Bring forth therefore fruits meet for repentance:" Matthew 3:8
  • joebert
  • Sledgehammer
  • Genius
  • No Avatar
  • Registrado: Feb 10, 2004
  • Mensajes: 13458
  • Loc: Florida
  • Status: Offline

Nota Mayo 14th, 2011, 8:16 am

Identificación empezar aquí, y tratar de recordar lo que estaba pensando cuando escribí esto.

Código: [ Select ]
// Allowing the class to be accessible throughout the design
   $tpl = $this;
  1. // Allowing the class to be accessible throughout the design
  2.    $tpl = $this;
Strong with this one, the sudo is.
  • Bogey
  • Bogey
  • Genius
  • Avatar de Usuario
  • Registrado: Jul 14, 2005
  • Mensajes: 8212
  • Loc: USA
  • Status: Offline

Nota Mayo 14th, 2011, 9:55 am

Ok :lol: Mi clase y hora actual es así:
PHP Código: [ Select ]
<?php
class time {
 
    function time()
    {
        $time = $this;
    }
 
    public function format_time($tmsp, $userID)
    {
        // Returning the formatted date/time
        return date('D M j, Y g:i a', $tmsp);
    }
}
?>
  1. <?php
  2. class time {
  3.  
  4.     function time()
  5.     {
  6.         $time = $this;
  7.     }
  8.  
  9.     public function format_time($tmsp, $userID)
  10.     {
  11.         // Returning the formatted date/time
  12.         return date('D M j, Y g:i a', $tmsp);
  13.     }
  14. }
  15. ?>
Funciona de esa manera :) Gracias
"Bring forth therefore fruits meet for repentance:" Matthew 3:8
  • Bogey
  • Bogey
  • Genius
  • Avatar de Usuario
  • Registrado: Jul 14, 2005
  • Mensajes: 8212
  • Loc: USA
  • Status: Offline

Nota Mayo 14th, 2011, 10:06 am

Ok, esto es raro...después de un segundo pensé que el tiempo ha cambiado la clase de mensaje anterior:
PHP Código: [ Select ]
<?php
class time {
 
    public function format_time($tmsp, $userID)
    {
        // Returning the formatted date/time
        return date('D M j, Y g:i a', $tmsp);
    }
}
?>
  1. <?php
  2. class time {
  3.  
  4.     public function format_time($tmsp, $userID)
  5.     {
  6.         // Returning the formatted date/time
  7.         return date('D M j, Y g:i a', $tmsp);
  8.     }
  9. }
  10. ?>
(Eso es lo que originalmente se inició con que estaba tirando el error)

y ahora funciona perfectamente bien...No tengo idea de qué decir...estúpido PHP.

He cambiado todo a lo que se inició con y no es tirar los errores y trabajando como debe...Que debe haber cambiado algo para que funcione, pero no sé lo que es...tal vez el hecho de que he cambiado $ tpl-> tiempo a $ tpl-> fecha ...¿Podría haberlo hecho?
"Bring forth therefore fruits meet for repentance:" Matthew 3:8
  • joebert
  • Sledgehammer
  • Genius
  • No Avatar
  • Registrado: Feb 10, 2004
  • Mensajes: 13458
  • Loc: Florida
  • Status: Offline

Nota Mayo 14th, 2011, 1:56 pm

Puede ser. Podría ser una buena idea tomarse un descanso, luego volver y pensar en lo que el código está haciendo una vez que haya olvidado lo que es eres haciendo.
Strong with this one, the sudo is.
  • Bogey
  • Bogey
  • Genius
  • Avatar de Usuario
  • Registrado: Jul 14, 2005
  • Mensajes: 8212
  • Loc: USA
  • Status: Offline

Nota Mayo 14th, 2011, 2:00 pm

joebert escribió:
Puede ser. Podría ser una buena idea tomarse un descanso, luego volver y pensar en lo que el código está haciendo una vez que haya olvidado lo que es eres haciendo.

Esto me recuerda...Ya he tenido tiempo de $ tpl-> configurado a la hora (); (i La razón por la que ha cambiado a la fecha $ tpl->)...lo extraño es que todavía no funciona después de que lo cambié a la fecha $ tpl-> (que lo cambié a la fecha $ tpl-> antes de hacer tu primer post aquí). Entonces empecé a cambiar la clase de tiempo y después lo cambió todo, puse todo de nuevo en la clase de tiempo y ahora funciona.


Creo que toma enferma tu consejo y tomar un descanso de ella. Voy a empezar a reskinning un foro phpBB3 ahora :lol: No PHP así que considero esto como una pausa.
"Bring forth therefore fruits meet for repentance:" Matthew 3:8
  • Nightslyr
  • Proficient
  • Proficient
  • No Avatar
  • Registrado: Sep 21, 2005
  • Mensajes: 274
  • Status: Offline

Nota Mayo 17th, 2011, 1:33 pm

ojo * contracción en más "global" parámetros *

Es posible que desee ver en la inyección de dependencia para este tipo de cosas.

Publicar Información

  • Total de mensajes en este tema: 10 mensajes
  • Usuarios navegando por este Foro: No hay usuarios registrados visitando el Foro y 176 invitados
  • No puede abrir nuevos temas en este Foro
  • No puede responder a temas en este Foro
  • No puede editar sus mensajes en este Foro
  • No puede borrar sus mensajes en este Foro
  • No puede enviar adjuntos en este Foro
 
 

© 2011 Unmelted, LLC. Ozzu® es una marca registrada de Unmelted, LLC