php-windows Digest 15 Apr 2006 22:27:16 -0000 Issue 2938
Topics (messages 26841 through 26848):
Problems with Database Singleton Class
26841 by: Vandegrift, Ken
26842 by: Martin Vidovic
26843 by: Vandegrift, Ken
Problems IE+PHPDev and Protecte pages
26844 by: Anton Heuschen
Re: PHP5 and MySQL
26845 by: Stephen Lake
26846 by: Stephen Lake
Re: php editors
26847 by: Stephen Lake
26848 by: pfancy
Administrivia:
To subscribe to the digest, e-mail:
[EMAIL PROTECTED]
To unsubscribe from the digest, e-mail:
[EMAIL PROTECTED]
To post to the list, e-mail:
[email protected]
----------------------------------------------------------------------
--- Begin Message ---
Hello,
I am trying to create a singleton database class that wraps up the
creation of the PDO class. However, when I call the getInstance()
method and run it through the gettype() method (for testing purposes) it
returns 'object' but when I try to create a statement object I recieve a
Fatal error: Call to undefined method WebDBConn::setAttribute()...
Here is my class:
/**
* Class WebDBConn (Singleton Pattern)
* This class ensures only one instance of a database
* connection is used throughout application
*/
class WebDBConn {
/**
* DB Instance
* @var object
* @access private
*/
private static $instance = NULL;
/**
* WebDBConn Constructor
* @return void
* @access private
*/
private function __construct() {
self::$instance = new PDO($db, $user, $pwd);
self::$instance->setAttribute(PDO::ATTR_ERRMODE,
PDO::ERRMODE_EXCEPTION);
}
/**
* Return DB instance or facilitate intitial connection
* @return object
* @access public
*/
public static function getInstance() {
if (!self::$instance) {
self::$instance = new WebDBConn;
}
return self::$instance;
}
}
Here is the code within my application:
$db = WebDBConn::getInstance();
$db->prepare($sql); (Fatal Error occurs right here -
WebDBConn::prepare() undefined method)
Shouldn't $db be an instance of PDO???
Any help would be greatly appreciated.
Ken Vandegrift
--- End Message ---
--- Begin Message ---
On Friday 14 of April 2006 17:51, Vandegrift, Ken wrote:
> class WebDBConn {
>
> private static $instance = NULL;
>
> private function __construct() {
>
> self::$instance = new PDO($db, $user, $pwd);
> self::$instance->setAttribute(PDO::ATTR_ERRMODE,
> PDO::ERRMODE_EXCEPTION);
> }
>
> public static function getInstance() {
>
> if (!self::$instance) {
> self::$instance = new WebDBConn;
Here you overwrite your instance of PDO class with WebDBConn instance.
> }
> return self::$instance;
> }
> }
What you should do, is put the $instance initialization which you have in
__construct(), to the getInstance() method in place of 'self::$instance = new
WebDBConn'.
__construct() can be empty as you do not need an instance of WebDBConn
(everything is static).
Martin
--- End Message ---
--- Begin Message ---
Thank You!
Yep, moving the DB creation logic into the getInstance method did the
trick.
I now have one instance of my DB connection to use throughout my
program.
New and improved (working) getInstance method:
/**
* Return DB instance or create intitial connection
* @return object (PDO)
* @access public
*/
public static function getInstance() {
if (!self::$instance) {
$settings = new Settings();
self::$instance = new PDO($db, $settings->id[$db],
$settings->pwd[$db]);
self::$instance->setAttribute(PDO::ATTR_ERRMODE,
PDO::ERRMODE_EXCEPTION);
}
return self::$instance;
}
Ken Vandegrift
-----Original Message-----
From: Martin Vidovic [mailto:[EMAIL PROTECTED]
Sent: Friday, April 14, 2006 10:58 AM
To: [email protected]
Subject: Re: [PHP-WIN] Problems with Database Singleton Class
On Friday 14 of April 2006 17:51, Vandegrift, Ken wrote:
> class WebDBConn {
>
> private static $instance = NULL;
>
> private function __construct() {
>
> self::$instance = new PDO($db, $user, $pwd);
> self::$instance->setAttribute(PDO::ATTR_ERRMODE,
> PDO::ERRMODE_EXCEPTION);
> }
>
> public static function getInstance() {
>
> if (!self::$instance) {
> self::$instance = new WebDBConn;
Here you overwrite your instance of PDO class with WebDBConn instance.
> }
> return self::$instance;
> }
> }
What you should do, is put the $instance initialization which you have
in __construct(), to the getInstance() method in place of
'self::$instance = new WebDBConn'.
__construct() can be empty as you do not need an instance of WebDBConn
(everything is static).
Martin
--
PHP Windows Mailing List (http://www.php.net/) To unsubscribe, visit:
http://www.php.net/unsub.php
--- End Message ---
--- Begin Message ---
Dear PHP list.
I have a website (local) running on a Windows box. I am using :
PhpDev5 (php,Mysql,Apache 1.x)
I am using PHLib - where I then make use of the template and page auth
functions of PHPLib
What I have found, and this is my problem.
The web application requires a login, all pages are protected, when you log in
and start using the system the system would at random and at no particular page
simply come up with the login screen, as if you are logged out.
THE STRANGE THING IS: this happens with IE, if I open the site in Opera or
FireFox then this never happens, I log in, work on the system for as long as I
need to and it will never loose my login session. Only IE does this. As I said,
I cant see a patern (time or certain page)
What I have tried is to set IE to handle its cache differently (but none of
these changes did anything) (i.e refresh page on every visit,auto, never etc)
I don’t know where else to look, this just seems strange.
Any help would be greatly appreciated.
Anton
[EMAIL PROTECTED]
--
No virus found in this outgoing message.
Checked by AVG Free Edition.
Version: 7.1.385 / Virus Database: 268.4.1/308 - Release Date: 4/11/2006
--- End Message ---
--- Begin Message ---
Try http://www.php-editors.com they list many different editors that you
can use.
HTH
Steve
""pfancy"" <[EMAIL PROTECTED]> wrote in message
news:[EMAIL PROTECTED]
> Can you show me some of the code i need to unquote so i get an idea about
> where it is.
--- End Message ---
--- Begin Message ---
Try http://www.php-editors.com they list many different editors that you
can use.
HTH
Steve
""pfancy"" <[EMAIL PROTECTED]> wrote in message
news:[EMAIL PROTECTED]
> Can you show me some of the code i need to unquote so i get an idea about
> where it is.
--- End Message ---
--- Begin Message ---
Try http://www.php-editors.com they list many different editors that you
can use....it lists freebies, commercialware and shareware
""pfancy"" <[EMAIL PROTECTED]> wrote in message
news:[EMAIL PROTECTED]
> I've been reading over php. I have bought php and mysql for dummies but my
> question is what kind of php editors can a person get where they can view
> what it looks like? or where do you find your php.ini file. i've been
> reading i need to work on that. and when that is fixed up to where i need
> it
> would i be able to view a php file like i would an html file? thanks.
--- End Message ---
--- Begin Message ---
Thanks for all the php editor idea. I have been going through them.
""Stephen Lake"" <[EMAIL PROTECTED]> wrote in message
news:[EMAIL PROTECTED]
> Try http://www.php-editors.com they list many different editors that you
> can use....it lists freebies, commercialware and shareware
>
> ""pfancy"" <[EMAIL PROTECTED]> wrote in message
> news:[EMAIL PROTECTED]
> > I've been reading over php. I have bought php and mysql for dummies but
my
> > question is what kind of php editors can a person get where they can
view
> > what it looks like? or where do you find your php.ini file. i've been
> > reading i need to work on that. and when that is fixed up to where i
need
> > it
> > would i be able to view a php file like i would an html file? thanks.
--- End Message ---