Create only one instance of Object

Hi all ,

I am using php version 4 which did not have private method access.
How to prevent the user call the object constructor as shown below :

Thanks
Joshua

class MYSQL_DB
{
var $dbConn = null;

/**
* php4 did not have private access method , only php5
* user can call the function, need to find way not allow it ,
but it better use getInstance() to
* have one database instance only
*/
function MYSQL_DB()
{
global $dbHost, $dbUser, $dbPass, $dbName;
$this->dbConn=&ADONewConnection("mysql"); # create a
connection
$this->dbConn->Connect($dbHost, $dbUser, $dbPass, $dbName);
}

/**
* [at] desc get only one instance created
*/
function &getInstance()
{
static $instance;

if (!isset($instance))
{
$instance=new MYSQL_DB;
}

return $instance;
}

function getConnection() { return $this->dbConn; }
}
weetat.yeo [ Fr, 09 März 2007 10:10 ] [ ID #1652902 ]

Re: Create only one instance of Object

weetat <weetat.yeo [at] gmail.com> wrote:

> Hi all ,
>
> I am using php version 4 which did not have private method access.
> How to prevent the user call the object constructor as shown below :

Enforcing a singleton in PHP4 is tricky. I usually end up with some pseu=
do =

code triggerering a (fatal) error in the constructor:

$info =3D reset(debug_backtrace());
if(!isset($info['class'])
|| $info['class'] !=3D get_class($this)
||!isset($info['type'])
||$info['type']!=3D'::'
||!isset($info['function'])
||$info['function']!=3D'getInstance'
){
trigger_error('This is a singleton class, only use =

class::getInstance()', E_USER_ERROR);
}

HTH
-- =

Rik Wasmus
Posted on Usenet, not any forum you might see this in.
Ask Smart Questions: http://tinyurl.com/anel
Rik [ Fr, 09 März 2007 13:48 ] [ ID #1652911 ]

Re: Create only one instance of Object

weetat wrote:
> Hi all ,
>
> I am using php version 4 which did not have private method access.
> How to prevent the user call the object constructor as shown below :
>
> Thanks
> Joshua
>
> class MYSQL_DB
> {
> var $dbConn = null;
>
> /**
> * php4 did not have private access method , only php5
> * user can call the function, need to find way not allow it ,
> but it better use getInstance() to
> * have one database instance only
> */
> function MYSQL_DB()
> {

if(_MYSQL_CLASS) { return; }
define(_MYSQL_CLASS,true);

> global $dbHost, $dbUser, $dbPass, $dbName;
> $this->dbConn=&ADONewConnection("mysql"); # create a
> connection
> $this->dbConn->Connect($dbHost, $dbUser, $dbPass, $dbName);
> }

should prevent anyone from using constructor as a function, even if you use
two objects of the same class, the constructor will only create one connection
together.


--

//Aho
Shion [ Fr, 09 März 2007 13:50 ] [ ID #1652912 ]

Re: Create only one instance of Object

J.O. Aho <user [at] example.net> wrote:
> if(_MYSQL_CLASS) { return; }
> define(_MYSQL_CLASS,true);

Hmmmz, allthough a cluttering of constants, a bit more elegant indeed. I'd
use defined(_MYSQL_CLASS) though, else it will never ever create an object
for you if not previously defined to false :P

--
Rik Wasmus
Posted on Usenet, not any forum you might see this in.
Ask Smart Questions: http://tinyurl.com/anel
Rik [ Fr, 09 März 2007 14:00 ] [ ID #1652913 ]

Re: Create only one instance of Object

Rik wrote:
> J.O. Aho <user [at] example.net> wrote:
>> if(_MYSQL_CLASS) { return; }
>> define(_MYSQL_CLASS,true);
>
> Hmmmz, allthough a cluttering of constants, a bit more elegant indeed.
> I'd use defined(_MYSQL_CLASS) though, else it will never ever create an
> object for you if not previously defined to false :P

You are so right, didn't think of that, don't use define that much myself and
those times I do, it's usually a string that I want to output.

--

//Aho
Shion [ Fr, 09 März 2007 14:44 ] [ ID #1652915 ]
PHP » alt.php » Create only one instance of Object

Vorheriges Thema: PHP and JAVA problem
Nächstes Thema: Bookmark and/or URL Manipulation