[PHP] green bean question on singleton php5

2008-01-16 Thread julian



Hi,

I am implementing this

class dbaccess{
  static $db=null;
  static $othervar=33;

  private  function dbaccess(){
dbaccess::$db= new mysqli(localhost,USER,PASSWD,DB);
if(mysqli_connect_errno()){
  echo no way;
}
  }

public static function GetDb(){
  if(dbaccess::$db==null){
  dbaccess::$db= new dbaccess();
  }
  return  dbaccess::$db;

  }
}


$db=dbaccess::GetDE();

$db-query(..);

will fail...with Call to undefined method dbaccess::query()

apparently $db is of type dbaccess... and thus has not does not have 
query implemented



any hhelp appreciated.

JCG

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] green bean question on singleton php5

2008-01-16 Thread Eric Butera
On Jan 16, 2008 12:57 PM, julian [EMAIL PROTECTED] wrote:


 Hi,

 I am implementing this

 class dbaccess{
static $db=null;
static $othervar=33;

private  function dbaccess(){
  dbaccess::$db= new mysqli(localhost,USER,PASSWD,DB);
  if(mysqli_connect_errno()){
echo no way;
  }
}

 public static function GetDb(){
if(dbaccess::$db==null){
dbaccess::$db= new dbaccess();
}
return  dbaccess::$db;

}
 }


 $db=dbaccess::GetDE();

 $db-query(..);

 will fail...with Call to undefined method dbaccess::query()

 apparently $db is of type dbaccess... and thus has not does not have
 query implemented


 any hhelp appreciated.

 JCG

 --
 PHP General Mailing List (http://www.php.net/)
 To unsubscribe, visit: http://www.php.net/unsub.php



Try changing dbaccess to self inside the class.

Look at this:
http://us.php.net/manual/en/language.oop5.patterns.php
Example#2 Singleton Function

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] green bean question on singleton php5

2008-01-16 Thread Daniel Brown
On Jan 16, 2008 12:57 PM, julian [EMAIL PROTECTED] wrote:


 Hi,

 I am implementing this
[snip!]

I'm heading out to lunch, so double-check this for errors, but I
rewrote your class.  You'll have to add your fetch handlers and such.

?
class dbaccess{
  static $db = null;
  static $conn = null;
  static $query = null;
  static $othervar = 33;

  private function self(){
dbaccess::$conn = new mysqli(HOST,DBNAME,PASS,USERNAME);
if(mysqli_connect_errno()){
  return False;
} else {
return dbaccess::$conn;
}
  }

  public function query($query){
dbaccess::$query = mysqli_query(dbaccess::self(),$query);
return dbaccess::$query;
  }

public static function GetDb(){
  if(dbaccess::$db==null){
  dbaccess::$db= new dbaccess();
  }
  return dbaccess::$db;

  }
}


$db=dbaccess::GetDb();
$db-query(..); // Your query here.
?

-- 
/Dan

Daniel P. Brown
Senior Unix Geek and #1 Rated Year's Coolest Guy By Self Since
Nineteen-Seventy-[mumble].

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] green bean question on singleton php5

2008-01-16 Thread Julian


but that forces me to implement a large interface of functions that I 
prefer to avoid...


the $dummy thing works... but I guess it is not by the book.

I would like to understand what am I missing fom the concept

Thanks.

JCG

Daniel Brown wrote:

On Jan 16, 2008 12:57 PM, julian [EMAIL PROTECTED] wrote:
  

Hi,

I am implementing this


[snip!]

I'm heading out to lunch, so double-check this for errors, but I
rewrote your class.  You'll have to add your fetch handlers and such.

?
class dbaccess{
  static $db = null;
  static $conn = null;
  static $query = null;
  static $othervar = 33;

  private function self(){
dbaccess::$conn = new mysqli(HOST,DBNAME,PASS,USERNAME);
if(mysqli_connect_errno()){
  return False;
} else {
return dbaccess::$conn;
}
  }

  public function query($query){
dbaccess::$query = mysqli_query(dbaccess::self(),$query);
return dbaccess::$query;
  }

public static function GetDb(){
  if(dbaccess::$db==null){
  dbaccess::$db= new dbaccess();
  }
  return dbaccess::$db;

  }
}


$db=dbaccess::GetDb();
$db-query(..); // Your query here.
?

  


--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] green bean question on singleton php5

2008-01-16 Thread Julian


nope... only works if I change

$dummy= new dbaccess();

and keep the rest .

Thanks.


... hope it does not repeat... got undelivered...

Eric Butera wrote:

On Jan 16, 2008 12:57 PM, julian [EMAIL PROTECTED] wrote:
  

Hi,

I am implementing this

class dbaccess{
   static $db=null;
   static $othervar=33;

   private  function dbaccess(){
 dbaccess::$db= new mysqli(localhost,USER,PASSWD,DB);
 if(mysqli_connect_errno()){
   echo no way;
 }
   }

public static function GetDb(){
   if(dbaccess::$db==null){
   dbaccess::$db= new dbaccess();
   }
   return  dbaccess::$db;

   }
}


$db=dbaccess::GetDE();

$db-query(..);

will fail...with Call to undefined method dbaccess::query()

apparently $db is of type dbaccess... and thus has not does not have
query implemented


any hhelp appreciated.

JCG

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php





Try changing dbaccess to self inside the class.

Look at this:
http://us.php.net/manual/en/language.oop5.patterns.php
Example#2 Singleton Function

  


--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] green bean question on singleton php5

2008-01-16 Thread Nathan Nobbe
to all who have posted classes w/ the singleton instance as a
public static; this is not good.
the singleton instance should be stored in a private static variable.
why?  because, otherwise client code can just access the value
directly, and even unset the instance; which sort of defeats the
purpose of singleton.

-nathan


RE: [PHP] green bean question on singleton php5

2008-01-16 Thread Andrés Robinet
 -Original Message-
 From: Julian [mailto:[EMAIL PROTECTED]
 Sent: Wednesday, January 16, 2008 3:37 PM
 To: Daniel Brown
 Cc: julian; php-general@lists.php.net
 Subject: Re: [PHP] green bean question on singleton php5
 
 
 but that forces me to implement a large interface of functions that I
 prefer to avoid...
 
 the $dummy thing works... but I guess it is not by the book.
 
 I would like to understand what am I missing fom the concept
 
 Thanks.
 
 JCG
 
 Daniel Brown wrote:
  On Jan 16, 2008 12:57 PM, julian [EMAIL PROTECTED] wrote:
 
  Hi,
 
  I am implementing this
 
  [snip!]
 
  I'm heading out to lunch, so double-check this for errors, but I
  rewrote your class.  You'll have to add your fetch handlers and such.
 
  ?
  class dbaccess{
static $db = null;
static $conn = null;
static $query = null;
static $othervar = 33;
 
private function self(){
  dbaccess::$conn = new mysqli(HOST,DBNAME,PASS,USERNAME);
  if(mysqli_connect_errno()){
return False;
  } else {
  return dbaccess::$conn;
  }
}
 
public function query($query){
  dbaccess::$query = mysqli_query(dbaccess::self(),$query);
  return dbaccess::$query;
}
 
  public static function GetDb(){
if(dbaccess::$db==null){
dbaccess::$db= new dbaccess();
}
return dbaccess::$db;
 
}
  }
 
 
  $db=dbaccess::GetDb();
  $db-query(..); // Your query here.
  ?
 
 

dbaccess has no query method as you are not inheriting from mysqli. So
dbaccess::$db will have no query method either. I don't even think you need
to construct objects of type dbaccess to make this work, after all, you
have only static methods and properties which are attached to the type and
not the instances. Also, dbaccess::$db should be mysqli::db instead.

Check the previous suggestions about creating either an inherited class, or
an accessor.

Regards,

Rob


Andrés Robinet | Lead Developer | BESTPLACE CORPORATION
5100 Bayview Drive 206, Royal Lauderdale Landings, Fort Lauderdale, FL 33308
| TEL 954-607-4207 | FAX 954-337-2695
Email: [EMAIL PROTECTED]  | MSN Chat: [EMAIL PROTECTED]  |  SKYPE:
bestplace |  Web: http://www.bestplace.biz | Web: http://www.seo-diy.com

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] green bean question on singleton php5

2008-01-16 Thread Jochem Maas

julian schreef:



Hi,

I am implementing this


try comparing this rewrite with your version:


abstract class dbaccess {
  static $db = null;

  private static function init() {
if (dbaccess::$db))
return;

dbaccess::$db = new mysqli(localhost,USER,PASSWD,DB);
if(mysqli_connect_errno())  
throw new Exception('cannot connect to DB');
  }

  public static function getDB() {
self::init();
return dbaccess::$db;
  }
}

try {
$db = dbaccess::getDB();
$db-query(SELECT `foo` FROM `bar` WHERE `qux`=1);
} catch (Exception) {
die($e-getMessage());
}


class dbaccess{
  static $db=null;
  static $othervar=33;

  private  function dbaccess(){

dbaccess::$db= new mysqli(localhost,USER,PASSWD,DB);
if(mysqli_connect_errno()){
  echo no way;
}
  }

public static function GetDb(){

  if(dbaccess::$db==null){


dbaccess is a static class, you should not be constructing it here it!


  dbaccess::$db= new dbaccess();
  }
  return  dbaccess::$db;

  }

}


$db=dbaccess::GetDE();

$db-query(..);

will fail...with Call to undefined method dbaccess::query()

apparently $db is of type dbaccess... and thus has not does not have 
query implemented



any hhelp appreciated.

JCG



--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] green bean question on singleton php5

2008-01-16 Thread Nathan Nobbe
On Jan 16, 2008 1:36 PM, Julian [EMAIL PROTECTED] wrote:

 I would like to understand what am I missing fom the concept


here are the issues i see;

you should have a private static for the instance of dbaccess
you should have a private instance variable for the instance of the mysqli
class
you should be able to programatically handle failure to connect to the
database

in the code i show here, you do not have to return the mysqli instance from
dbaccess::GetDb()
if you prefer, that method would return the instance to the singleton, then
you would
have a public accessor method that would return the instance of the mysqli
class.
this is the simpler of the 2 options.

class dbaccess{
  private static $instance = null;  // instance of dbaccess
  public static $othervar=33;   // dont know what this is for, but you
can keep it if you want
  private $mySqliConn = null;  // instance of mysqli

  private  function dbaccess() {
$this-mySqliConn = new mysqli(localhost,USER,PASSWD,DB);
if(mysqli_connect_errno()) {
  throw new RuntimeException(mysqli_connect_error());
}
  }

public static function GetDb(){
  if(dbaccess::$instance == null) {
 dbaccess::$instance = new dbaccess();
  }
  return  $this-mySqliConn;
  }
}

-nathan


RE: [PHP] green bean question on singleton php5

2008-01-16 Thread Andrés Robinet
 -Original Message-
 From: Julian [mailto:[EMAIL PROTECTED]
 Sent: Wednesday, January 16, 2008 3:39 PM
 Cc: php-general@lists.php.net
 Subject: Re: [PHP] green bean question on singleton php5
 
 
 nope... only works if I change
 
 $dummy= new dbaccess();
 
 and keep the rest .
 
 Thanks.
 
 
 ... hope it does not repeat... got undelivered...
 
 Eric Butera wrote:
  On Jan 16, 2008 12:57 PM, julian [EMAIL PROTECTED] wrote:
 
  Hi,
 
  I am implementing this
 
  class dbaccess{
 static $db=null;
 static $othervar=33;
 
 private  function dbaccess(){
   dbaccess::$db= new mysqli(localhost,USER,PASSWD,DB);
   if(mysqli_connect_errno()){
 echo no way;
   }
 }
 
  public static function GetDb(){
 if(dbaccess::$db==null){
 dbaccess::$db= new dbaccess();
 }
 return  dbaccess::$db;
 
 }
  }
 
 
  $db=dbaccess::GetDE();
 
  $db-query(..);
 
  will fail...with Call to undefined method dbaccess::query()
 
  apparently $db is of type dbaccess... and thus has not does not have
  query implemented
 
 
  any hhelp appreciated.
 
  JCG
 
  --
  PHP General Mailing List (http://www.php.net/)
  To unsubscribe, visit: http://www.php.net/unsub.php
 
 
 
 
  Try changing dbaccess to self inside the class.
 
  Look at this:
  http://us.php.net/manual/en/language.oop5.patterns.php
  Example#2 Singleton Function
 
 

Not the ideal solution, but this is what I meant:

class dbaccess {

  private static $db = null;
  
  public static function GetDb() {
if (!isset(self::$db)) {
self::$db = new mysqli(localhost, USER, PASSWD, DB);
if(mysqli_connect_errno()) {
  die(Very bad, you need to handle errors better);
}
}
return self::$db;
  }
}

$db = dbaccess::GetDb();
$stmt = $db-query('SHOW DATABASES');

print_r($stmt); // Outputs mysqli_result Object ( )

Regards,

Rob

Andrés Robinet | Lead Developer | BESTPLACE CORPORATION
5100 Bayview Drive 206, Royal Lauderdale Landings, Fort Lauderdale, FL 33308
| TEL 954-607-4207 | FAX 954-337-2695
Email: [EMAIL PROTECTED]  | MSN Chat: [EMAIL PROTECTED]  |  SKYPE:
bestplace |  Web: http://www.bestplace.biz | Web: http://www.seo-diy.com

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] green bean question on singleton php5

2008-01-16 Thread julian


you are forcing the no instantiation via abstract, instead of hiding via 
private method constructor.


You change the constructor for an init function.

still the $dummy = new dbaccess (). looks like a simpler solution

Thanks for your comments



Jochem Maas wrote:

julian schreef:



Hi,

I am implementing this


try comparing this rewrite with your version:


abstract class dbaccess {
  static $db = null;

  private static function init() {
if (dbaccess::$db))
return;

dbaccess::$db = new mysqli(localhost,USER,PASSWD,DB);
if(mysqli_connect_errno())
throw new Exception('cannot connect to DB');

  }

  public static function getDB() {
self::init();
return dbaccess::$db;
  }
}

try {
$db = dbaccess::getDB();
$db-query(SELECT `foo` FROM `bar` WHERE `qux`=1);
} catch (Exception) {
die($e-getMessage());
}


class dbaccess{
  static $db=null;
  static $othervar=33;
  private  function dbaccess(){
dbaccess::$db= new mysqli(localhost,USER,PASSWD,DB);
if(mysqli_connect_errno()){
  echo no way;
}
  }
public static function GetDb(){
  if(dbaccess::$db==null){


dbaccess is a static class, you should not be constructing it here it!


  dbaccess::$db= new dbaccess();
  }
  return  dbaccess::$db;
  }
}


$db=dbaccess::GetDE();

$db-query(..);

will fail...with Call to undefined method dbaccess::query()

apparently $db is of type dbaccess... and thus has not does not have 
query implemented



any hhelp appreciated.

JCG



--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] green bean question on singleton php5

2008-01-16 Thread Jochem Maas

julian schreef:


you are forcing the no instantiation via abstract, instead of hiding via 
private method constructor.


you want to garantee a single instance of the mysqli object - who cares
exactly how this is done. besides which the whole exercise is bogus. you
want a DB connection abstraction that allows to create connections to
multiple DBs and given a given set of connection parameters to always
return the same connection object ... which is not exactly the same as
a singleton (at all)

given that dbaccess doesn't extend mysqli instantiation of dbaccess is 
completely
pointless no?



You change the constructor for an init function.

still the $dummy = new dbaccess (). looks like a simpler solution


no idea what you mean, personally I can't see anything complicated in any of 
these
examples - I couldn't find a post showing something with $dummy.



Thanks for your comments



Jochem Maas wrote:

julian schreef:



Hi,

I am implementing this


try comparing this rewrite with your version:


abstract class dbaccess {
  static $db = null;

  private static function init() {
if (dbaccess::$db))
return;

dbaccess::$db = new mysqli(localhost,USER,PASSWD,DB);
if(mysqli_connect_errno())throw new Exception('cannot 
connect to DB');

  }

  public static function getDB() {
self::init();
return dbaccess::$db;
  }
}

try {
$db = dbaccess::getDB();
$db-query(SELECT `foo` FROM `bar` WHERE `qux`=1);
} catch (Exception) {
die($e-getMessage());
}


class dbaccess{
  static $db=null;
  static $othervar=33;
  private  function dbaccess(){
dbaccess::$db= new mysqli(localhost,USER,PASSWD,DB);
if(mysqli_connect_errno()){
  echo no way;
}
  }
public static function GetDb(){
  if(dbaccess::$db==null){


dbaccess is a static class, you should not be constructing it here it!


  dbaccess::$db= new dbaccess();
  }
  return  dbaccess::$db;
  }
}


$db=dbaccess::GetDE();

$db-query(..);

will fail...with Call to undefined method dbaccess::query()

apparently $db is of type dbaccess... and thus has not does not have 
query implemented



any hhelp appreciated.

JCG





--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] green bean question on singleton php5

2008-01-16 Thread Nathan Nobbe
On Jan 16, 2008 3:59 PM, Jochem Maas [EMAIL PROTECTED] wrote:

 given that dbaccess doesn't extend mysqli instantiation of dbaccess is
 completely
 pointless no?


i dont know; i think using an instance of dbaccess to control a single
instance of the
mysqli class is appropriate.  personally, i wouldnt extend mysqli unless i
had a good
reason to.  i would probly only rationalize this if i wanted to override
some of the behavior
in a specific way, or extend it in some meaningful way (even then i might
use composition).
if that isnt the intention i think composition is the best choice for the
singleton.
more a matter of preference than anything i suppose.  at least i cant think
of a better reason
to argue for composition.


 no idea what you mean


ya; some of these posts from you are a little hard to understand, julian.

-nathan


Re: [PHP] green bean question on singleton php5

2008-01-16 Thread Eric Butera
On Jan 16, 2008 4:13 PM, Nathan Nobbe [EMAIL PROTECTED] wrote:
 On Jan 16, 2008 3:59 PM, Jochem Maas [EMAIL PROTECTED] wrote:

  given that dbaccess doesn't extend mysqli instantiation of dbaccess is
  completely
  pointless no?


 i dont know; i think using an instance of dbaccess to control a single
 instance of the
 mysqli class is appropriate.  personally, i wouldnt extend mysqli unless i
 had a good
 reason to.  i would probly only rationalize this if i wanted to override
 some of the behavior
 in a specific way, or extend it in some meaningful way (even then i might
 use composition).
 if that isnt the intention i think composition is the best choice for the
 singleton.
 more a matter of preference than anything i suppose.  at least i cant think
 of a better reason
 to argue for composition.


  no idea what you mean


 ya; some of these posts from you are a little hard to understand, julian.

 -nathan


I still don't understand the obsession of a singleton in regards to a
db connection.  Using a registry is a much better practice I think.

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] green bean question on singleton php5

2008-01-16 Thread Jochem Maas

Eric Butera schreef:

On Jan 16, 2008 4:13 PM, Nathan Nobbe [EMAIL PROTECTED] wrote:

On Jan 16, 2008 3:59 PM, Jochem Maas [EMAIL PROTECTED] wrote:


given that dbaccess doesn't extend mysqli instantiation of dbaccess is
completely
pointless no?


i dont know; i think using an instance of dbaccess to control a single
instance of the
mysqli class is appropriate.  personally, i wouldnt extend mysqli unless i
had a good
reason to.  i would probly only rationalize this if i wanted to override
some of the behavior
in a specific way, or extend it in some meaningful way (even then i might
use composition).
if that isnt the intention i think composition is the best choice for the
singleton.
more a matter of preference than anything i suppose.  at least i cant think
of a better reason
to argue for composition.



no idea what you mean


ya; some of these posts from you are a little hard to understand, julian.

-nathan



I still don't understand the obsession of a singleton in regards to a
db connection.  Using a registry is a much better practice I think.



I think I alluded to the registry pattern in my reply above - although I'm not
sure. could you care to ellaborate what you mean by registry?

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] green bean question on singleton php5

2008-01-16 Thread Eric Butera
On Jan 16, 2008 4:55 PM, Jochem Maas [EMAIL PROTECTED] wrote:
 Eric Butera schreef:

  On Jan 16, 2008 4:13 PM, Nathan Nobbe [EMAIL PROTECTED] wrote:
  On Jan 16, 2008 3:59 PM, Jochem Maas [EMAIL PROTECTED] wrote:
 
  given that dbaccess doesn't extend mysqli instantiation of dbaccess is
  completely
  pointless no?
 
  i dont know; i think using an instance of dbaccess to control a single
  instance of the
  mysqli class is appropriate.  personally, i wouldnt extend mysqli unless i
  had a good
  reason to.  i would probly only rationalize this if i wanted to override
  some of the behavior
  in a specific way, or extend it in some meaningful way (even then i might
  use composition).
  if that isnt the intention i think composition is the best choice for the
  singleton.
  more a matter of preference than anything i suppose.  at least i cant think
  of a better reason
  to argue for composition.
 
 
  no idea what you mean
 
  ya; some of these posts from you are a little hard to understand, julian.
 
  -nathan
 
 
  I still don't understand the obsession of a singleton in regards to a
  db connection.  Using a registry is a much better practice I think.
 

 I think I alluded to the registry pattern in my reply above - although I'm not
 sure. could you care to ellaborate what you mean by registry?


Sure.  A registry is just a place to store things.  So create an
instance of your DB and stick it in the registry and you're done.
Then make your script rely on the registry so you can swap stuff out
that way instead of having a hard coded reference to some static
method that cannot be replaced at runtime.

Also with the registry you can use lazy loading.  My scripts don't
create an instance of the db class until it is actually used.  It has
the ability to create one at any time by calling the
fancy_namespace_Registry::get('db') which will read some config I
enabled upstream with the fancy_namespace_Registry::lazy('db',
array(config to pass)).  In testing I can just replace the db entry
with a mock and life is good.

Also, my database doesn't connect to the server until an actual method
is called that uses it.  So just layers of just in time.

Of course you could argue that using static methods in the registry is
bad too, but I gotta draw the line somewhere! :)  There isn't really a
right or wrong.  Just different ways of doing things.  I used to use
the singleton with my DB but it became troublesome to test plus always
making sure the dependences were pre-loaded was annoying.  Now
everything takes care of itself.

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] green bean question on singleton php5

2008-01-16 Thread Nathan Nobbe
On Jan 16, 2008 4:55 PM, Jochem Maas [EMAIL PROTECTED] wrote:

 Eric Butera schreef:
  I still don't understand the obsession of a singleton in regards to a
  db connection.  Using a registry is a much better practice I think.
 

 I think I alluded to the registry pattern in my reply above - although I'm
 not
 sure. could you care to ellaborate what you mean by registry?


though i havent seen registry defined in any of the few patterns books ive
seen;
i think its supposed to be a list of singletons, essentially; if you want a
'global'
registry that is.  and i suppose the registry class would itself be a
singleton as well.
here is a somewhat concrete definition / explanation / example..
http://www.phppatterns.com/doku.php/design/the_registry

-nathan


Re: [PHP] green bean question on singleton php5

2008-01-16 Thread Nathan Nobbe
On Jan 16, 2008 5:06 PM, Eric Butera [EMAIL PROTECTED] wrote:

 Also with the registry you can use lazy loading.


singleton is typically implemented with a lazy loading approach, and most
of the code samples ive seen on this thread today use a lazy loading
approach.

could you give us a more concrete example of what youre defining as a
registry,
eric?

thanks,

-nathan


Re: [PHP] green bean question on singleton php5

2008-01-16 Thread Eric Butera
On Jan 16, 2008 5:06 PM, Nathan Nobbe [EMAIL PROTECTED] wrote:
 On Jan 16, 2008 4:55 PM, Jochem Maas [EMAIL PROTECTED] wrote:

  Eric Butera schreef:
 
 
 
 
   I still don't understand the obsession of a singleton in regards to a
   db connection.  Using a registry is a much better practice I think.
  
 
  I think I alluded to the registry pattern in my reply above - although I'm
 not
  sure. could you care to ellaborate what you mean by registry?
 

 though i havent seen registry defined in any of the few patterns books ive
 seen;
 i think its supposed to be a list of singletons, essentially; if you want a
 'global'
 registry that is.  and i suppose the registry class would itself be a
 singleton as well.
 here is a somewhat concrete definition / explanation / example..
 http://www.phppatterns.com/doku.php/design/the_registry

 -nathan



Here is an implementation:
http://framework.zend.com/manual/en/zend.registry.html

Here is another:
http://www.stubbles.net/browser/trunk/src/main/php/net/stubbles/util/stubRegistry.php

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] green bean question on singleton php5

2008-01-16 Thread Nathan Nobbe
On Jan 16, 2008 5:09 PM, Eric Butera [EMAIL PROTECTED] wrote:

 Here is an implementation:
 http://framework.zend.com/manual/en/zend.registry.html

 Here is another:

 http://www.stubbles.net/browser/trunk/src/main/php/net/stubbles/util/stubRegistry.php



cool; ill have a look when i get home.

-nathan


Re: [PHP] green bean question on singleton php5

2008-01-16 Thread Jochem Maas

Eric Butera schreef:

On Jan 16, 2008 4:55 PM, Jochem Maas [EMAIL PROTECTED] wrote:

Eric Butera schreef:


On Jan 16, 2008 4:13 PM, Nathan Nobbe [EMAIL PROTECTED] wrote:

On Jan 16, 2008 3:59 PM, Jochem Maas [EMAIL PROTECTED] wrote:


given that dbaccess doesn't extend mysqli instantiation of dbaccess is
completely
pointless no?

i dont know; i think using an instance of dbaccess to control a single
instance of the
mysqli class is appropriate.  personally, i wouldnt extend mysqli unless i
had a good
reason to.  i would probly only rationalize this if i wanted to override
some of the behavior
in a specific way, or extend it in some meaningful way (even then i might
use composition).
if that isnt the intention i think composition is the best choice for the
singleton.
more a matter of preference than anything i suppose.  at least i cant think
of a better reason
to argue for composition.



no idea what you mean

ya; some of these posts from you are a little hard to understand, julian.

-nathan


I still don't understand the obsession of a singleton in regards to a
db connection.  Using a registry is a much better practice I think.


I think I alluded to the registry pattern in my reply above - although I'm not
sure. could you care to ellaborate what you mean by registry?



Sure.  A registry is just a place to store things.  So create an
instance of your DB and stick it in the registry and you're done.
Then make your script rely on the registry so you can swap stuff out
that way instead of having a hard coded reference to some static
method that cannot be replaced at runtime.

Also with the registry you can use lazy loading.  My scripts don't
create an instance of the db class until it is actually used.  It has
the ability to create one at any time by calling the
fancy_namespace_Registry::get('db') which will read some config I
enabled upstream with the fancy_namespace_Registry::lazy('db',
array(config to pass)).  In testing I can just replace the db entry
with a mock and life is good.

Also, my database doesn't connect to the server until an actual method
is called that uses it.  So just layers of just in time.

Of course you could argue that using static methods in the registry is
bad too, but I gotta draw the line somewhere! :)  There isn't really a
right or wrong.  Just different ways of doing things.  I used to use
the singleton with my DB but it became troublesome to test plus always
making sure the dependences were pre-loaded was annoying.  Now
everything takes care of itself.


that makes it clear. thank you.

I guess what I was alluding to was a DB [connection object] specific registry.
I guess I have never actually thought about it enough because reading your 
explaination
makes it blatantly obvious that the 'pattern' can be made so much more generic 
...
and I love the generic lazy loading mechanism - I'll be using that idea 
sharpish :)

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] green bean question on singleton php5

2008-01-16 Thread Eric Butera
On Jan 16, 2008 5:10 PM, Nathan Nobbe [EMAIL PROTECTED] wrote:
 On Jan 16, 2008 5:09 PM, Eric Butera [EMAIL PROTECTED] wrote:

  Here is an implementation:
  http://framework.zend.com/manual/en/zend.registry.html
 
  Here is another:
 
 http://www.stubbles.net/browser/trunk/src/main/php/net/stubbles/util/stubRegistry.php
 


 cool; ill have a look when i get home.

 -nathan


A more to the point example would be the Solar implementation that has
the lazy loading I keep ranting about.

http://solarphp.com/manual/Registry

Sorry for the noise. :)

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php