Re: OOP database tables <-> php interface (semi LONG)
"Toby A Inkster" <usenet200703@t obyinkster.co.u kschreef in bericht
news:r1s3f4-4kp.ln1@ophelia .g5n.co.uk...
Aaah yes of course, I read about such an example at one point in time. And
at first I didn't understand how such an object could ever be made since the
constructor is private. So I left it for what it was. But the object is of
course constructed from within the object itself with its singleton method.
Doh! ;-)
It all makes sense now. Thanks Toby.
"Toby A Inkster" <usenet200703@t obyinkster.co.u kschreef in bericht
news:r1s3f4-4kp.ln1@ophelia .g5n.co.uk...
amygdala wrote:
>
>
OK -- how about this -- you have a Database class, but you want to make
sure that your scripts only ever open a single connection to the database.
You don't want both, say, a User object and an Article object to open
their own individual connections like this:
>
<?php
class User
{
private $db;
public function __construct ()
{
$this->db = new Database(/*...*/);
}
}
class Article
{
private $db;
public function __construct ()
{
$this->db = new Database(/*...*/);
}
}
?>
>
And so to prevent this, you make the Database's constructor function
private. This is known as the "singleton" design pattern in the PHP
manual. (It's on the same page as the factory design pattern!)
>
<?php
class Database
{
public function query ($q) { /*...*/ }
public function type () { /*...*/ }
public function escape_string ($s) { /*...*/ }
private function __construct ($dsn, $username, $password) { /*...*/ }
>
private static $instance;
public static singleton ($dsn, $username, $password)
{
if (!isset(self::$ instance))
self::$instance = new Database ($dsn, $username, $password);
>
return self::$instance ;
}
}
?>
>
So now you must use "Database::sing leton()" instead of "new Database" to
get a database object, but the singleton class method will always return a
pointer to the same Database object -- it won't create a brand new one!
>
And your code becomes:
>
<?php
class User
{
private $db;
public function __construct ()
{
$this->db = Database::singl eton(/*...*/);
}
}
class Article
{
private $db;
public function __construct ()
{
$this->db = Database::singl eton(/*...*/);
}
}
?>
>
>
>Could you by any chance provide the simplest example of an occassion
>where making the constructor private makes sense? Cause I can't think of
>a valuable situation right now.
>where making the constructor private makes sense? Cause I can't think of
>a valuable situation right now.
OK -- how about this -- you have a Database class, but you want to make
sure that your scripts only ever open a single connection to the database.
You don't want both, say, a User object and an Article object to open
their own individual connections like this:
>
<?php
class User
{
private $db;
public function __construct ()
{
$this->db = new Database(/*...*/);
}
}
class Article
{
private $db;
public function __construct ()
{
$this->db = new Database(/*...*/);
}
}
?>
>
And so to prevent this, you make the Database's constructor function
private. This is known as the "singleton" design pattern in the PHP
manual. (It's on the same page as the factory design pattern!)
>
<?php
class Database
{
public function query ($q) { /*...*/ }
public function type () { /*...*/ }
public function escape_string ($s) { /*...*/ }
private function __construct ($dsn, $username, $password) { /*...*/ }
>
private static $instance;
public static singleton ($dsn, $username, $password)
{
if (!isset(self::$ instance))
self::$instance = new Database ($dsn, $username, $password);
>
return self::$instance ;
}
}
?>
>
So now you must use "Database::sing leton()" instead of "new Database" to
get a database object, but the singleton class method will always return a
pointer to the same Database object -- it won't create a brand new one!
>
And your code becomes:
>
<?php
class User
{
private $db;
public function __construct ()
{
$this->db = Database::singl eton(/*...*/);
}
}
class Article
{
private $db;
public function __construct ()
{
$this->db = Database::singl eton(/*...*/);
}
}
?>
>
at first I didn't understand how such an object could ever be made since the
constructor is private. So I left it for what it was. But the object is of
course constructed from within the object itself with its singleton method.
Doh! ;-)
It all makes sense now. Thanks Toby.
Comment