An object within an object problem

  • Bogey
  • Genius
  • Genius
  • Bogey
  • Posts: 8388
  • Loc: USA

Post 3+ Months Ago

I got the following code
PHP Code: [ 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. }

and then in then in the template file, I try to access it like
PHP Code: [ Select ]
<?=$tpl->time->format_time($c, $d);?>

That is throwing me the following error:
Quote:
Fatal error: Call to a member function format_time() on a non-object in C:\wamp\www\CMS\style\default\viewpost.php on line 24

How do I fix that error?

When I do print_r($this->time) or var_dump($this->time) in template class it tells me it's an object.
var_dump
Quote:
object(time)#15 (0) { }

print_r
Quote:
time Object ( )
  • Anonymous
  • Bot
  • No Avatar
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post 3+ Months Ago

  • WritingBadCode
  • Graduate
  • Graduate
  • User avatar
  • Posts: 214
  • Loc: Sweden

Post 3+ Months Ago

I don't know about the following:

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

Try:

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

OR:

$tpl = new time();
$tpl->format_time($c, $d);
  • Bogey
  • Genius
  • Genius
  • Bogey
  • Posts: 8388
  • Loc: USA

Post 3+ Months Ago

The thing with $tpl is that $tpl holds the template class. I can't have it be set to a different object.

Reason this baffles me because it always worked for me... maybe I just forgot how to do that.
  • Bogey
  • Genius
  • Genius
  • Bogey
  • Posts: 8388
  • Loc: USA

Post 3+ Months Ago

Sorry, I guess I shouldn't be so secretive as to what my classes contain :lol:

template.php
PHP Code: [ 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 Code: [ 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. }

My usage
PHP Code: [ Select ]
<?=$tpl->time->format_time($column['threadPostDate'], $column['userID']);?>
  • joebert
  • Fart Bubbles
  • Genius
  • User avatar
  • Posts: 13502
  • Loc: Florida

Post 3+ Months Ago

I'd start here, and try to remember what I was thinking when I wrote this.

Code: [ 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;
  • Bogey
  • Genius
  • Genius
  • Bogey
  • Posts: 8388
  • Loc: USA

Post 3+ Months Ago

Ok :lol: My time class now looks like:
PHP Code: [ 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. ?>
It works that way :) Thanks
  • Bogey
  • Genius
  • Genius
  • Bogey
  • Posts: 8388
  • Loc: USA

Post 3+ Months Ago

Ok, this is weird... after a second's thought I changed the time class from previous post to:
PHP Code: [ 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. ?>
(That is what I originally started with that was throwing the error)

and it now works perfectly fine... I have no idea what to say... stupid PHP.

I changed everything back to what I started with and it isn't throwing any errors and working like it should... I MUST have changed something to make it work, but I don't know what it is... maybe the fact that I changed $tpl->time to $tpl->date ... Could that have done it?
  • joebert
  • Fart Bubbles
  • Genius
  • User avatar
  • Posts: 13502
  • Loc: Florida

Post 3+ Months Ago

Could be. Might be a good idea to take a break, then come back and think about what the code is doing once you've forgotten what it is you're doing.
  • Bogey
  • Genius
  • Genius
  • Bogey
  • Posts: 8388
  • Loc: USA

Post 3+ Months Ago

joebert wrote:
Could be. Might be a good idea to take a break, then come back and think about what the code is doing once you've forgotten what it is you're doing.

This reminds me... I already had $tpl->time set to time(); (The reason i changed it to $tpl->date)... the weird thing is it still didn't work after I changed it to $tpl->date (I changed it to $tpl->date before you made your first post here). Then I started changing the time class and after I changed everything, I put everything back in the time class and now it works.


I think I'll take your advice and take a break from it. I'm going to start reskinning a phpbb3 forum right now :lol: Not PHP so I consider this as taking a break.
  • Nightslyr
  • Proficient
  • Proficient
  • Nightslyr
  • Posts: 283

Post 3+ Months Ago

*eye twitch at more 'global' parameters*

You may want to look into dependency injection for this kind of thing.

Post Information

  • Total Posts in this topic: 10 posts
  • Users browsing this forum: No registered users and 78 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
cron
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.