Page 1 of 1
External php script globals problem
Posted: Thu 19. Aug 2004, 15:55
by jamba
I am trying to call a shopping basket php app that uses a class like this:
Code: Select all
class js_jambashop {
// properties available
var $js_strBasketSessionID;
var $js_lngSiteID;
function js_jambashop() {
// global vars required in this class
global $js_strServerName;
global $js_strServerUserName;
global $js_strServerPassword;
global $js_strDatabaseName;
global $js_lngSiteID;
global $js_strSiteCookieName;
global $js_strErrorLocation;
etc... etc... etc...
This works fine as a standalone php script but when I try to use this using the php replacement tag, the global values defined here are not available - they are all empty.
Is this a limitation of the CMS?

Posted: Thu 19. Aug 2004, 21:04
by Oliver Georgi
I think "class" can not be used within {PHP} replacement tag. Maybe add the class to index.php.
Oliver
Posted: Thu 19. Aug 2004, 21:23
by jamba
Thanks for the suggestion, Oliver.
I've already tried two steps to eliminate the possibility. First, I included the php file in index.php by way of the require_once() function, and still no joy.
Secondly, I edited the class file so that instead of trying to connect to a database with a value passed via a global, it simple performed an echo("Hello World") function.
This worked, which demonstrates that calling a class is possible. The problem seems to do with trying to register globals within the class.
Any ideas?!?

Posted: Thu 19. Aug 2004, 21:45
by Oliver Georgi
send me the script if possible.
I always use:
$GLOBAL not global.
I see session stuff there too - and you said db connections. This might be tproblematic too.
Oliver
Posted: Thu 19. Aug 2004, 22:02
by jamba
Thanks, Oliver.
Here's a stripped down version of the files involved. The first is a config file, which I have at /jamba/config.php...
Code: Select all
// site config
$js_strServerName = 'localhost'; // database server
I need to access these from the class file, which I've stored at /jamba/class.php
Code: Select all
class js_jambashop {
function js_jambashop() {
// global vars required in this class
global $js_strServerName;
// configure this class
$this -> js_strServerName = $js_strServerName;
}
function js_test() {
// get the global value and print it out
echo("SERVER NAME = " . $this -> js_strServerName);
die();
}
}
I call this by using the code below...
Code: Select all
require_once ('/jamba/config.php');
require_once ('/jamba/class.php');
// instantiate the class
$js_jambashop = new js_jambashop;
// test function
$js_jambashop -> js_test();
But all I get from this is...
SERVER NAME =
My php skills are by no means polished, so I may well be going about this the wrong way!
Thanks again, Oliver. Fantastic CMS!
Posted: Thu 19. Aug 2004, 22:39
by Oliver Georgi
define config vars like:
$GLOBALS['myvar'] = 'myvalue';
Oliver
Posted: Thu 19. Aug 2004, 23:04
by jamba
Thanks, Oliver.
Doing it like this seems to have solved the problem...
Config.php...
Code: Select all
// site config
$GLOBALS ['js_strServerName'] = 'localhost'; // database server
Class.php...
Code: Select all
class js_jambashop {
function js_jambashop() {
// configure this class
$this -> js_strServerName = $GLOBALS['js_strServerName'];
}
function js_test() {
// get the global value and print it out
echo("MY SERVER NAME = " . $this -> js_strServerName);
die();
}
}
File included in CMS...
require_once ('/jamba/config.php');
require_once ('/jamba/class.php');
// instantiate the class
// instantiate the class
$js_jambashop = new js_jambashop;
// test function
$js_jambashop -> js_test();
Produces this...
MY SERVER NAME = localhost