djones Posted October 2, 2008 Share Posted October 2, 2008 I keep getting this error: Parse error: syntax error, unexpected '.', expecting ',' or ';' in /mypath/classes/cDB.php on line 17 Line 17 is: protected $ConnectionString = 'username=' . $this->user . ';password=' . $this->pass . ';database=' . $this->server . ';'; I need all the ';' in the line to parse through the settings for Oracle. I was using this, but I'm doing away with $_GLOBALS[]. And this works ok: $GLOBALS['ConnectionString'] = 'username='.$_SESSION['user'].';password='.$_SESSION['pass'].';database='.$server.';'; Link to comment https://forums.phpfreaks.com/topic/126762-help-using-protected-in-class/ Share on other sites More sharing options...
KevinM1 Posted October 2, 2008 Share Posted October 2, 2008 Try constructing it with double quotes: protected $ConnectionString = "username={$this->user};password={$this->pass};database={$this->server};"; Link to comment https://forums.phpfreaks.com/topic/126762-help-using-protected-in-class/#findComment-655634 Share on other sites More sharing options...
jcrocker Posted October 2, 2008 Share Posted October 2, 2008 It might be because you are using variables when initiating $ConnectionString. This can be solved by setting it inside a method, to replicate the behaviour you want put it inside the construct. class connection { protected $user = 'root'; protected $pass = 'root'; protected $server = 'localhost'; protected $ConnectionString; public function construct() { $this->ConnectionString = 'username=' . $this->user . ';password=' . $this->pass . ';database=' . $this->server . ';'; } } Link to comment https://forums.phpfreaks.com/topic/126762-help-using-protected-in-class/#findComment-655639 Share on other sites More sharing options...
djones Posted October 2, 2008 Author Share Posted October 2, 2008 @jcrocker : How will I access $ConnectionString when extending it to another class? This errors in Oracle class cQuery extends connection { private $oOracleConnection; function __construct(){ $this->oOracleConnection = new OracleConnection($this->ConnectionString); $this->oOracleConnection->Open(); } function __destruct(){ $this->oOracleConnection->Close(); $this->oOracleConnection = null; } } Link to comment https://forums.phpfreaks.com/topic/126762-help-using-protected-in-class/#findComment-655675 Share on other sites More sharing options...
jcrocker Posted October 2, 2008 Share Posted October 2, 2008 Using the method I showed you the construct of the parent object does not get called automatically, and so in the construct of the child object call the parents construct manually. class cQuery extends connection { private $oOracleConnection; function __construct() { parent::__construct(); $this->oOracleConnection = new OracleConnection($this->ConnectionString); $this->oOracleConnection->Open(); } } And remember you can do the same for the destruct (or any other over-ridden method). Link to comment https://forums.phpfreaks.com/topic/126762-help-using-protected-in-class/#findComment-655825 Share on other sites More sharing options...
aschk Posted October 3, 2008 Share Posted October 3, 2008 Indeed the problem exists in that you're attempting to use "->$var" from inside an object that has not been instantiated yet. The variable initialisation private/protected/public that you have inside your class need to use static variables (i.e. strings/numerics). Unlike C++ PHP cannot provide dynamic references to internal variables/functions unless the object is initialiased. Link to comment https://forums.phpfreaks.com/topic/126762-help-using-protected-in-class/#findComment-656199 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.