Reply to Re: OOP database tables <-> php interface (semi LONG)

Your name:

Reply:


Posted by amygdala on 04/13/07 16:56

"Toby A Inkster" <usenet200703@tobyinkster.co.uk> schreef in bericht
news:r1s3f4-4kp.ln1@ophelia.g5n.co.uk...
> amygdala wrote:
>
>> 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.
>
> 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::singleton()" 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::singleton(/*...*/);
> }
> }
> class Article
> {
> private $db;
> public function __construct ()
> {
> $this->db = Database::singleton(/*...*/);
> }
> }
> ?>
>

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.

[Back to original message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация