Jump to content

Help using protected in class


djones

Recommended Posts

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

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 . ';';
}
}

 

 

@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;
	}

}

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).

 

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.