View previous topic :: View next topic |
Author |
Message |
louis11 Smarty Rookie
Joined: 29 Jul 2008 Posts: 11
|
Posted: Wed Jul 30, 2008 9:41 pm Post subject: Fatal Error on smarty object |
|
|
I have been experimenting with SMARTY for a few days, and so far I haven't had any problems. Today however, I keep getting intermittent:
Quote: |
Fatal error: Call to a member function assign() on a non-object in /Users/city.php on line 41
|
I've searched Google, and these forums and found several people recommending turning the SMARTY object into a global variable. I tried this, and still nothing.
The file in questions is as follows:
Code: |
<?
// include configuration files
require_once('includes/configuration.php');
// get action
$action = strip_tags($_GET['action']);
// add new city
if($action == 'add')
{
$smarty -> assign('sub_title', 'Add City');
$smarty -> display('pages/city_add.tpl');
}
// modify existing city
else if($action == 'modify')
{
$smarty -> assign('sub_title', 'Modify City');
$smarty -> display('pages/city_modify.tpl');
}
// remove city
else if($action == 'remove')
{
$smarty -> assign('sub_title', 'Remove City');
$smarty -> display('pages/city_remove.tpl');
}
// show navigation
else
{
$smarty -> assign('sub_title', 'City Management');
$smarty -> display('pages/city.tpl');
}
?>
|
configuration.php contains the smarty object:
Code: |
<?
// include all library files
require_once('libs/Smarty.class.php');
// setup SMARTY
global $smarty;
$smarty = new Smarty();
$smarty -> template_dir = 'templates';
$smarty -> compile_dir = 'templates_c';
$smarty -> cache_dir = 'cache';
$smarty -> config_dir = 'configs';
$smarty -> security = true;
?>
|
Also, i've tried several variations of initiating a new smarty object (i've read a few articles, and they all varied):
Code: |
$smarty = new smarty;
$smarty = new smarty();
$smarty = new Smarty;
$smarty = new Smarty();
|
Any help would be appreciated. |
|
Back to top |
|
U.Tews Administrator
Joined: 22 Nov 2006 Posts: 5068 Location: Hamburg / Germany
|
Posted: Wed Jul 30, 2008 10:02 pm Post subject: |
|
|
Add global $smarty; also in your main script. |
|
Back to top |
|
louis11 Smarty Rookie
Joined: 29 Jul 2008 Posts: 11
|
Posted: Wed Jul 30, 2008 10:17 pm Post subject: |
|
|
Just did that. Now I can access city.php, but other files are still affected (I added global $smarty; to the top of them too)
For instance, the account.php file:
Code: |
<?
// include configuration files
require_once('includes/configuration.php');
global $smarty;
// if not logged in, redirect to login
if(!$_SESSION['username'])
{
session_destroy();
header("Location: index.php");
}
// get blips on map
$city = array();
$sql = "select location_x, location_y, name, id from city";
$sql = mysql_query($sql) or die(mysql_error());
// add blips to city array
while($row = mysql_fetch_assoc($sql))
{
$location = array('x' => $row['location_x'], 'y' => $row['location_y'], 'name' => $row['name'], 'id' => $row['id']);
array_push($city, $location);
}
// add blips to template
$smarty -> assign('cities', $city);
// show big map
$smarty -> display('index.tpl');
?>
|
Gives this error "Fatal error: Call to a member function assign() on a non-object in /Users/city.php on line 43" |
|
Back to top |
|
U.Tews Administrator
Joined: 22 Nov 2006 Posts: 5068 Location: Hamburg / Germany
|
Posted: Wed Jul 30, 2008 10:36 pm Post subject: |
|
|
Hmm Strange |
|
Back to top |
|
louis11 Smarty Rookie
Joined: 29 Jul 2008 Posts: 11
|
Posted: Wed Jul 30, 2008 10:56 pm Post subject: |
|
|
Yea It appears as though every file is working correctly, except city.php. In fact, if city.php invokes any function calls on any smarty object the entire web application dies and spits out that error about city.php.
When I take out all code from city.php, everything works just fine. I copied and pasted all of the code from account.php (which works -- when there isn't anything in city.php) and again it broke.
So, it looks like nothing SMARTY related can be done in city.php . . . what am I doing wrong?
update
I just copied the code from city.php to test.php. I removed everything from city.php (except the <? ?> tags) and everything works fine. I can access every page, including test.php.
Ah never mind, it worked for a second and now it's broken again. |
|
Back to top |
|
U.Tews Administrator
Joined: 22 Nov 2006 Posts: 5068 Location: Hamburg / Germany
|
Posted: Wed Jul 30, 2008 11:54 pm Post subject: |
|
|
The error message says that the $smarty object is not existent. But configuration.php does create it.....
The error must be in your script, because it appears when you try to call a smarty methode.
Are your scripts loacted in the same folder? |
|
Back to top |
|
louis11 Smarty Rookie
Joined: 29 Jul 2008 Posts: 11
|
Posted: Thu Jul 31, 2008 12:01 am Post subject: |
|
|
U.Tews wrote: | The error message says that the $smarty object is not existent. But configuration.php does create it.....
The error must be in your script, because it appears when you try to call a smarty methode.
Are your scripts loacted in the same folder? |
Yea everything is located in the same folder. In fact, I thought that I might not be calling configuration.php correctly, so I changed it to some arbitrary file and got an "error: file doesn't exist".
My directory tree is as follows:
[DIR] cache
[DIR] configs
[DIR] includes
------- configuration.php
[DIR] templates
[DIR] templates_c
account.php
index.php
city.php
I agree with you though, the smarty object isn't being initialized. If I copy and paste the SMARTY object from configuration.php (below) to the file(s) in question it works.
Code: |
<?
// setup SMARTY
global $smarty;
$smarty = new Smarty;
$smarty->cache_lifetime = 1800;
$smarty -> template_dir = 'templates';
$smarty -> compile_dir = 'templates_c';
$smarty -> cache_dir = 'cache';
$smarty -> config_dir = 'configs';
$smarty -> security = true;
?>
|
So, the question: why isn't it creating the object? |
|
Back to top |
|
U.Tews Administrator
Joined: 22 Nov 2006 Posts: 5068 Location: Hamburg / Germany
|
Posted: Thu Jul 31, 2008 12:10 am Post subject: |
|
|
I currently have no idea.... |
|
Back to top |
|
louis11 Smarty Rookie
Joined: 29 Jul 2008 Posts: 11
|
Posted: Thu Jul 31, 2008 3:58 am Post subject: |
|
|
U.Tews wrote: | I currently have no idea.... |
Well thanks anyway If you come up with anything I would GREATLY appreciate it. Perhaps it's a bug with SMARTY?
On a side note, I did a print_r($smarty) on the page in question (with and without global $smarty) and it prints out all of the object information about the smarty object. So, I know that the smarty object is being created and passed over . . .
I also remembered that I had turned globals off in my php.ini file, so I turned that on and restarted Apache . . . and still nothing |
|
Back to top |
|
mohrt Administrator
Joined: 16 Apr 2003 Posts: 7368 Location: Lincoln Nebraska, USA
|
Posted: Thu Jul 31, 2008 2:13 pm Post subject: |
|
|
The error says it happens on line 41 of /Users/city.php, and the code you posted doesn't even have that many lines. Did you post the right code? |
|
Back to top |
|
louis11 Smarty Rookie
Joined: 29 Jul 2008 Posts: 11
|
Posted: Thu Jul 31, 2008 7:27 pm Post subject: |
|
|
mohrt wrote: | The error says it happens on line 41 of /Users/city.php, and the code you posted doesn't even have that many lines. Did you post the right code? |
Just comments at the top. Other than that, yea it's all there. Also one thing I noticed is that it gives an error about city.php regardless of which file you are accessing (e.g. account.php)
Last edited by louis11 on Thu Jul 31, 2008 7:49 pm; edited 1 time in total |
|
Back to top |
|
U.Tews Administrator
Joined: 22 Nov 2006 Posts: 5068 Location: Hamburg / Germany
|
Posted: Thu Jul 31, 2008 7:46 pm Post subject: |
|
|
So something must be calling city.php....
Do your templates include any PHP code? They should not. Just fishing....
Set
ini_set('display_errors', 1);
error_reporting(E_ALL);
in your scripts to see if this is providing more error details. |
|
Back to top |
|
mohrt Administrator
Joined: 16 Apr 2003 Posts: 7368 Location: Lincoln Nebraska, USA
|
Posted: Thu Jul 31, 2008 7:52 pm Post subject: |
|
|
I'm pretty sure I know what's going on. include_once() will not include anything if you already included it somewhere up the line of PHP files, and the $smarty object is not in scope at this point. Try this.
Make the configuration.php file look like this:
Code: | <?php
class mysmarty
{
public static function &instance()
{
static $smarty_obj = null;
if(!isset($smarty_obj))
{
// include all library files
require_once('libs/Smarty.class.php');
// setup SMARTY
$smarty_obj = new Smarty();
$smarty_obj -> template_dir = 'templates';
$smarty_obj -> compile_dir = 'templates_c';
$smarty_obj -> cache_dir = 'cache';
$smarty_obj -> config_dir = 'configs';
$smarty_obj -> security = true;
}
return $smarty_obj;
}
}
?> |
And then in all of your files:
Code: | require_once('includes/configuration.php');
$smarty = mysmarty::instance(); |
This should guarantee you get an instance of the smarty object, and the same instance each time it is retrieved. |
|
Back to top |
|
U.Tews Administrator
Joined: 22 Nov 2006 Posts: 5068 Location: Hamburg / Germany
|
Posted: Thu Jul 31, 2008 7:59 pm Post subject: |
|
|
mohrt this does not explain why the errormessage is prom city.php regarless which script was called. very strange.... |
|
Back to top |
|
louis11 Smarty Rookie
Joined: 29 Jul 2008 Posts: 11
|
Posted: Thu Jul 31, 2008 8:07 pm Post subject: |
|
|
mohrt wrote: | I'm pretty sure I know what's going on. include_once() will not include anything if you already included it somewhere up the line of PHP files, and the $smarty object is not in scope at this point. Try this.
Make the configuration.php file look like this:
Code: | <?php
class mysmarty
{
public static function &instance()
{
static $smarty_obj = null;
if(!isset($smarty_obj))
{
// include all library files
require_once('libs/Smarty.class.php');
// setup SMARTY
$smarty_obj = new Smarty();
$smarty_obj -> template_dir = 'templates';
$smarty_obj -> compile_dir = 'templates_c';
$smarty_obj -> cache_dir = 'cache';
$smarty_obj -> config_dir = 'configs';
$smarty_obj -> security = true;
}
return $smarty_obj;
}
}
?> |
And then in all of your files:
Code: | require_once('includes/configuration.php');
$smarty = mysmarty::instance(); |
This should guarantee you get an instance of the smarty object, and the same instance each time it is retrieved. |
This works! The only problem now is that it displays all of my templates Not exactly sure why this is . . . or rather, three templates instead of the one displayed. Is this a common error?
I suppose that if I can figure out why all of these templates are getting displayed that may also resolve why I get the error message regardless of what script is called?
Edit
I changed city.php to:
Code: |
// include configuration files
require_once('includes/configuration.php');
$smarty = mysmarty::instance();
// get action
$action = strip_tags($_GET['action']);
switch($action)
{
case 'add':
print "add";
break;
case 'modify':
print "modify";
break;
case 'remove':
print "remove";
break;
default:
print "works";
break;
}
|
And now when I access account.php I get the main template, plus "worksworks" at the top. (I have similar code on customer.php so I pressume it's calling customer.php along with city.php). Maybe having a common SMARTY object is causing the problem somehow? Just a thought.
Last edited by louis11 on Thu Jul 31, 2008 8:25 pm; edited 3 times in total |
|
Back to top |
|
|