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

Your name:

Reply:


Posted by Toby A Inkster on 04/13/07 08:36

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(/*...*/);
}
}
?>

I bet if you think about it there are plenty of occasions when the
singleton design pattern would have come in handy if you'd known about it!

> That OOP stuff is pretty hard to grasp for me

Don't worry about it. I think a lot of PHPers are at that stage now. PHP
4's object oriented capabilities were fairly limited, so apart from the
benefit of namespacing (i.e. being able to wrap up a bunch of functions and
variables into something with just one name) objects didn't offer an awful
lot of benefit over, say, arrays. A couple of years after its release, PHP
5 is only really beginning to get decent penetration onto commercial web
servers.

I'm quite lucky in that I have direct physical access to most of the PHP
servers I use, and quite a lot of say over the ones I don't have physical
access to. So I upgraded most of them to PHP 5 when it was still fairly
new (I waited until 5.0.4 I think. Most are now on 5.1.x and I'm looking
to switch to 5.2.x after another release or two), so I've been able to play
with the improved OO facilities for quite a while. But I'm still learning
things too!

--
Toby A Inkster BSc (Hons) ARCS
Contact Me ~ http://tobyinkster.co.uk/contact
Geek of ~ HTML/SQL/Perl/PHP/Python*/Apache/Linux

* = I'm getting there!

[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

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