[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1323
  • Last Modified:

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.


0
http:// thevpn.guru
Asked:
http:// thevpn.guru
  • 5
  • 3
1 Solution
 
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
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
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

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

  • 5
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now