Patch: Conflict when PEAR and a user created class DB is available

Markus Lanthaler markus at silverstripe.com
Tue Aug 7 03:18:35 PDT 2007


I found a conflict and made a little patch for it. SQLStore.php tries to 
include DB.php to check if PEAR is available.
When there is loaded already a class "DB" (which happens quite often), but 
also PEAR is available you'll get the following error:

Fatal error: Cannot redeclare class DB in ...

I created two simple patches since I didn't know which one is the better 
one. I prefer the second one because it eliminates the gloabal variable and 
doesn't require that the user specifies a constant, but feel free to choose 
one of the two:

The first one introduces a new constant Auth_OpenID_PEAR_AVAILABLE which can 
be set to FALSE to solve the problem:

{
hunk ./Auth/OpenID/SQLStore.php 24
-$__Auth_OpenID_PEAR_AVAILABLE = @include_once 'DB.php';
+if(defined('Auth_OpenID_PEAR_AVAILABLE') && !Auth_OpenID_PEAR_AVAILABLE) {
+    $__Auth_OpenID_PEAR_AVAILABLE = false;
+} else {
+    $__Auth_OpenID_PEAR_AVAILABLE = @include_once 'DB.php';
+}
+
}


The second one checks if a class DB is already declared and doesn't include 
DB.php if so. Then it tries to set the fetch mode only if the relevant 
constant is set:

{
hunk ./Auth/OpenID/SQLStore.php 23
-global $__Auth_OpenID_PEAR_AVAILABLE;
-$__Auth_OpenID_PEAR_AVAILABLE = @include_once 'DB.php';
+if((version_compare(phpversion(), "5.0.0", "<") && !class_exists('DB')) ||
+   (version_compare(phpversion(), "5.0.0", ">=") &&
+    !class_exists('DB', false))) {
+    // The class DB doesn't exists yet, try to include it
+    @include_once 'DB.php';
+}
+
hunk ./Auth/OpenID/SQLStore.php 91
-        global $__Auth_OpenID_PEAR_AVAILABLE;
-
hunk ./Auth/OpenID/SQLStore.php 113
-        if ($__Auth_OpenID_PEAR_AVAILABLE) {
-            $this->connection->setFetchMode(DB_FETCHMODE_ASSOC);
+        if (defined('DB_FETCHMODE_ASSOC')) {
+            @$this->connection->setFetchMode(DB_FETCHMODE_ASSOC);
}



Markus Lanthaler 




More information about the Dev mailing list