|
Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon. |
|
View previous topic :: View next topic |
Author |
Message |
lepra Smarty Rookie
Joined: 01 Jun 2003 Posts: 6
|
Posted: Mon Jun 02, 2003 12:28 am Post subject: php_include and global functions |
|
|
Maybe this is more of a php-question, but I'm quite sure it depends on the smarty php_include command.
My situation is like this:
I have a class with this class and function in classes.php:
[php:1:d3b58c0ddc]
class sql
{
var $result;
(and some other variables)
(some other functions here)
function sql_query($query)
{
$this->result = @mysql_query($query,$this->sqllink);
$this->error = mysql_error();
return $this->result;
}
}
[/php:1:d3b58c0ddc]
I include the code above in the main script called index.php with: include("classes.php")
and after that i create an instance with:
$dbcon = new sql;
and later in index.php i include another script called style.php which contains the php code that creates a smarty instance ($smarty = new Smarty;) and then assigns some values and stuff and then displays the template.
This template contains the php_include command which is used to dynamically include different php scripts that only contains php.
And it's here the problem starts.
The functions I use in these php_included scripts doesn't recognize the functions like the sql_query() function i showed at the top of this message.
It only gives me this error message:
Fatal error: Call to a member function on a non-object in /home/mjaumjau/public_html/code/statistics.php on line 3
I have figured out that it has something to do with global variables, and i have read many examples and tried, so now i'm asking for someone to help me to make this function sql_query() global. Excatly what do i type? and where??
I have found two bad solutions that i don't want:
1. make a new instance in every included php script.
2. access variables like this: $result1 = $GLOBALS['dbcon->sql_query("select * from something")'];
I use smarty 2.5.0
and I also wonder if there might be some setting in smarty to make all variables global? |
|
Back to top |
|
jorgerpo Smarty n00b
Joined: 15 Jun 2003 Posts: 3
|
Posted: Sun Jun 15, 2003 1:17 am Post subject: |
|
|
I have exactly the same question
|
|
Back to top |
|
boots Administrator
Joined: 16 Apr 2003 Posts: 5611 Location: Toronto, Canada
|
Posted: Sun Jun 15, 2003 5:49 am Post subject: |
|
|
I don't use php_include, so someone else will likely give you a better answer. Anyhow, if you declare $dbcon global BEFORE you initialize it (ie.
global $dbcon;
$dbcon = new sql;
and then add a global $dbcon to all the php_includes that use it... does that not work? I think it should. |
|
Back to top |
|
jorgerpo Smarty n00b
Joined: 15 Jun 2003 Posts: 3
|
Posted: Mon Jun 16, 2003 4:37 am Post subject: |
|
|
haven't tried that one
but it worked when I globalized the object in the included php file.
thanks.
Out of curiousity, why you don't use php_include??? |
|
Back to top |
|
boots Administrator
Joined: 16 Apr 2003 Posts: 5611 Location: Toronto, Canada
|
Posted: Mon Jun 16, 2003 5:01 am Post subject: |
|
|
jorgerpo wrote: | haven't tried that one
but it worked when I globalized the object in the included php file.
thanks.
Out of curiousity, why you don't use php_include??? |
I'm glad it worked for you
I don't use php_include because I don't need to. I do all of my application work before I pass anything to Smarty. That helps me keep PHP code and references out of my templates. I do however pass objects into Smarty, but as I mentioned, I set the objects up prior to invoking Smarty. I prefer such a style, though I'm not trying to suggest one is better than the other |
|
Back to top |
|
lepra Smarty Rookie
Joined: 01 Jun 2003 Posts: 6
|
Posted: Mon Jun 16, 2003 10:28 am Post subject: |
|
|
i forgot to answer but it worked for me too
thanx a lot.
I could avoid using php_include, but i had to include many different pages in php that i didn't have time to rewrite in smarty. and i didn't want to include them in the php-script that uses smarty, cause then i hade to use two templates (two $smarty->display()) and i just wanted to have one template to make it easier for the designers who will use the template.
i just wanted to say, that in some cases you just have to use a not so good-looking solution. well, sometimes it's just easier. :] |
|
Back to top |
|
|
|
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 vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|