Insert a variable value into a php class

In the script extract below, I need to insert a few values in to the variables of the class, the way I did it I am getting the following error :

Parse error: syntax error, unexpected T_VARIABLE in

<?php
$start='/home/alinux/public_html/';
$url = $_SERVER[HTTP_HOST];
$cut = explode('.',$url);
$url = $cut[0];

class JConfig {
        var $offline = '0';
        var $editor = 'jce';
        var $list_limit = '50';
        var $helpurl = 'http://help.joomla.org';

        var $log_path = $start.'/'.$url.'/logs';
        var $tmp_path = $start.'/'.$url./tmp';

Please assist.


LVL 19
http:// thevpn.guruAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

karoldvlCommented:
This won't work. You can't define:
var $log_path = $start.'/'.$url.'/logs';

as at that moment the value of $start and $url is not known (it's dynamic). There are two ways to do what you want - either instantiate the class and operate on the object or use a static property:
# --- method 1
<?php
$start='/home/alinux/public_html/';
$url = $_SERVER[HTTP_HOST];
$cut = explode('.',$url);
$url = $cut[0];

class JConfig {
        public $offline = '0';
        public $editor = 'jce';
        public $list_limit = '50';
        public $helpurl = 'http://help.joomla.org';

        public $log_path = '';
        public $tmp_path = '';
}

$myConfig = new JConfig();
$myConfig->log_path = $start.'/'.$url.'/logs';
$myConfig->tmp_path = $start.'/'.$url.'/tmp';

echo $myConfig->log_path;
echo $myConfig->tmp_path;
        
?>

# --- method 2
<?php
$start='/home/alinux/public_html/';
$url = $_SERVER[HTTP_HOST];
$cut = explode('.',$url);
$url = $cut[0];

class JConfig {
        public $offline = '0';
        public $editor = 'jce';
        public $list_limit = '50';
        public $helpurl = 'http://help.joomla.org';

        public static $log_path = '';
        public static $tmp_path = '';
}

JConfig::$log_path = $start.'/'.$url.'/logs';
JConfig::$tmp_path = $start.'/'.$url.'/tmp';

echo JConfig::$log_path;
echo JConfig::$tmp_path;
        
?>

Open in new window

0
http:// thevpn.guruAuthor Commented:
Basically method 2 is what I need. However, I am getting this error


Fatal error: Access to undeclared static property: JConfig::$log_path in /home/alinux/domains/wkplaza.com/public_html/webside-ekstrastor-orginal/configuration.php on line 63

JConfig::$log_path = $start.'/'.$url.'/logs';
JConfig::$tmp_path = $start.'/'.$url.'/tmp';
JConfig::$live_site = $url.'.wkplaza.com';
JConfig::$db = $url;

echo JConfig::$log_path;
echo JConfig::$tmp_path;
echo JConfig::$live_site;
echo JConfig::$db;
0
karoldvlCommented:
Are you sure you've declared $log_path as static?
0
Cloud Class® Course: Amazon Web Services - Basic

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

http:// thevpn.guruAuthor Commented:
I did add the static, however the variable is only being read in the same page and when the same instance of class is called in other php files/pages it is not seeing the static value.
0
karoldvlCommented:
I'm not sure I understand. Please provide a code snippet. Are you still seeing the error (access to undeclared static property)?
0
http:// thevpn.guruAuthor Commented:
Hi  The static error is gone but I am getting this error
jtablesession::Store Failed
DB function failed with error number 1046
No database selected SQL=INSERT INTO `jos_session` ( `session_id`,`time`,`username`,`gid`,`guest`,`client_id` ) VALUES ( 'f43bb69b0c0783b993a0b7afea42d7f0','1270926278','','0','1','0' )

This means that there is no database defined, but I did define it as instructed above.
0
karoldvlCommented:
Sorry, I missed the point completely. I didn't see it's not some abstract JConfig class you're talking about, but it's specific Joomla. I will update you in a minute.

0
karoldvlCommented:
OK, so here's the point.

It's a Joomla class, so you can't play with it freely. There are some expectations on how it should look like, so that's why you couldn't fiddle with static properties.

If you insist on providing dynamic configuration to the class, you should move this part to a custom constructor function. See if the attached code does the job. It's PHP5, if you need version 4 compatibility some rewrites would be needed.

<?php

define(START, '/home/alinux/public_html/');

class JConfig {
        public $offline = '0';
        public $editor = 'jce';
        public $list_limit = '50';
        public $helpurl = 'http://help.joomla.org';

        public $log_path = '';
        public $tmp_path = '';
        
        function __construct() {
			$url = $_SERVER[HTTP_HOST];
			$cut = explode('.',$url);
			$url = $cut[0];
			
			$this->log_path = START.'/'.$url.'/logs';
			$this->tmp_path = START.'/'.$url.'/logs';
		}
}
        
?>

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.