Hi,
can you not use a require_once within the __construct() function of a class when the required script it set as a variable (i.e. require_once($this->_db_connection_vars))
Thanks.
| SitePoint Sponsor |



Hi,
can you not use a require_once within the __construct() function of a class when the required script it set as a variable (i.e. require_once($this->_db_connection_vars))
Thanks.
Sure, require_once() should be fine with a __construct, why do you think it wouldn't be?
@AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.



Does the variable have scope within the class? Could you post a quick sample?
@AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.



This is the class
This is the script requesting the classPHP Code:class __cms{
// Define variables for use with cms
private $_hDB; // use for connection to mysql server
public $_root;
public $_urlroot;
public $_mysql_connection_vars;
public function __construct(){
require_once($this->_mysql_connection_vars);
}
}
PHP Code:$_cms = new __cms();
$_cms->_mysql_connection_vars = '../dbsettings.php';
You are setting the path for $_mysql_connection_vars AFTER the class is instantiated, __construct runs once you specify 'new __cms' and at that point, $_mysql_connection_vars has no value.
@AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.
An alternative would be:-
PHP Code:<?php
class __cms
{
// Define variables for use with cms
private $_hDB; // use for connection to mysql server
public $_root;
public $_urlroot;
public $_mysql_connection_vars;
public function __construct( $dbPath )
{
$this->_mysql_connection_vars = $dbPath;
require_once($this->_mysql_connection_vars);
}
}
new __cms( '../dbsettings.php' );
?>
@AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.



Ah, i didnt think of that lol, thanks.
No worries, although , if your trying to create a OOP CMS maybe you should be passing your Database object to the CMS?
@AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.



Whilst there is a lot to go into when creating an application like this, you would ideally want to create separate objects for say ''Database','Settings','Template' and such...
It would allow you to change a certain object without changing any of the other object, say moving from MySQL to Oracle, or from Smarty to your own templating engine.
Here is a very crass, rough and dirty example.
PHP Code:<?php
class Database
{
private $oHandle;
public function __construct( $sHost, $sUser, $sPass, $sName, $iPort )
{
$this->oHandle = new mysqli( $sHost, $sUser, $sPass, $sName, $iPort );
}
public function query( $sSQL )
{
return $this->oHandle->query( $sSQL );
}
public function escape( $sValue )
{
return $this->oHandle->real_escape_string( $sValue );
}
}
class CMS
{
protected $oDatabase;
public function __construct(Database $oDatabase)
{
$this->oDatabase = $oDatabase;
}
public function getUserDetails( $iID )
{
$oResult = $this->oDatabase->query(sprintf("SELECT username,password,name,sex FROM users WHERE id = %s;",(Integer)$iID));
/*
//--> Do stuff with result and return array.
*/
}
}
//--> Usage
try
{
$oDatabase = new Database( 'myserver','me','mypassword','cms_db',3306 );
$oCMS = new CMS( $oDatabase );
}
catch( Exception $oException )
{
die('Sorry, there is no database connection at this time.');
}
?>
@AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.
Bookmarks