note 108041 added to function.define
| From: | kriscraig@php.net | Date: | Fri, 23 Mar 2012 21:47:19 +0000 |
| Subject: | note 108041 added to function.define | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-186798@lists.php.net to get a copy of this message | ||
Some people like to have an ambiguous, engine-agnostic database include specified by assigning a
single config variable to one of a series of constants. Unfortunately, this can easily become
needlessly clunky if these defines are handled in an included config.php file, since more than one
hook will throw an ugly "already defined" error.
Here's a simple way to accomplish this architectural model without having to create a bunch of
clumsy sanity checks that compromise scalability:
<?php
/* Available databases. --Kris */
$dbengines = array();
$dbengines[] = "mysql";
$dbengines[] = "mysqli";
$dbengines[] = "pgsql";
foreach ( $dbengines as $engine )
{
if ( !defined( "_" . strtoupper( $engine ) . "_" ) )
{
define( "_" . strtoupper( $engine ) . "_", $engine );
}
}
/* Which database engine shall we use? --Kris */
$sql_type = _MYSQLI_;
?>
Then, somewhere later in the stack....
<?php
/* Include the file with the desired sql class. --Kris */
require_once( $sql_type . ".php" );
/* Now you can use the same code for any supported db engine. --Kris */
$sql = new sql();
$res = $sql->query( .... );
----
Server IP: 64.71.164.2
Probable Submitter: 173.10.123.113
----
Manual Page -- http://www.php.net/manual/en/function.define.php
Edit -- https://master.php.net/note/edit/108041
Del: integrated -- https://master.php.net/note/delete/108041/integrated
Del: useless -- https://master.php.net/note/delete/108041/useless
Del: bad code -- https://master.php.net/note/delete/108041/bad+code
Del: spam -- https://master.php.net/note/delete/108041/spam
Del: non-english -- https://master.php.net/note/delete/108041/non-english
Del: in docs -- https://master.php.net/note/delete/108041/in+docs
Del: other reasons-- https://master.php.net/note/delete/108041
Reject -- https://master.php.net/note/reject/108041
Search -- https://master.php.net/manage/user-notes.php