External php script globals problem

Get help with installation and running phpwcms here. Please do not post bug reports or feature requests here.
Post Reply
jamba
Posts: 50
Joined: Fri 23. Apr 2004, 11:18
Location: UK
Contact:

External php script globals problem

Post 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? :cry:
Russ Back :D
Jamba Solutions
http://jambasolutions.com
User avatar
Oliver Georgi
Site Admin
Posts: 9907
Joined: Fri 3. Oct 2003, 22:22
Contact:

Post by Oliver Georgi »

I think "class" can not be used within {PHP} replacement tag. Maybe add the class to index.php.

Oliver
Oliver Georgi | phpwcms Developer | GitHub | LinkedIn | Систрон
jamba
Posts: 50
Joined: Fri 23. Apr 2004, 11:18
Location: UK
Contact:

Post 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?!? :(
Russ Back :D
Jamba Solutions
http://jambasolutions.com
User avatar
Oliver Georgi
Site Admin
Posts: 9907
Joined: Fri 3. Oct 2003, 22:22
Contact:

Post 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
Oliver Georgi | phpwcms Developer | GitHub | LinkedIn | Систрон
jamba
Posts: 50
Joined: Fri 23. Apr 2004, 11:18
Location: UK
Contact:

Post 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!
Russ Back :D
Jamba Solutions
http://jambasolutions.com
User avatar
Oliver Georgi
Site Admin
Posts: 9907
Joined: Fri 3. Oct 2003, 22:22
Contact:

Post by Oliver Georgi »

define config vars like:

$GLOBALS['myvar'] = 'myvalue';

Oliver
Oliver Georgi | phpwcms Developer | GitHub | LinkedIn | Систрон
jamba
Posts: 50
Joined: Fri 23. Apr 2004, 11:18
Location: UK
Contact:

Post 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
Russ Back :D
Jamba Solutions
http://jambasolutions.com
Post Reply