From fd1d2b86ffac93a0d6d88581ef9a1e27f469fb04 Mon Sep 17 00:00:00 2001 From: David Grudl Date: Sun, 11 Nov 2007 04:59:39 +0000 Subject: [PATCH] added NClass --- dibi/dibi.php | 9 +------ dibi/libs/DibiResult.php | 22 +++++++--------- dibi/libs/NObject.php | 32 ++++++++++++----------- examples/connect.php | 51 ++++++++++++++++++++++++------------- examples/date.type.demo.php | 2 +- examples/logger.php | 4 +-- 6 files changed, 63 insertions(+), 57 deletions(-) diff --git a/dibi/dibi.php b/dibi/dibi.php index 349a9c8a..90a7d7dc 100644 --- a/dibi/dibi.php +++ b/dibi/dibi.php @@ -71,7 +71,7 @@ interface DibiVariableInterface * * @version $Revision$ $Date$ */ -class dibi +class dibi extends NClass { /** * Column type in relation to PHP native type @@ -157,13 +157,6 @@ class dibi - /** - * Monostate class - */ - final private function __construct() - {} - - /** * Creates a new DibiDriver object and connects it to specified database * diff --git a/dibi/libs/DibiResult.php b/dibi/libs/DibiResult.php index 110b13f5..b3e96c59 100644 --- a/dibi/libs/DibiResult.php +++ b/dibi/libs/DibiResult.php @@ -453,28 +453,24 @@ abstract class DibiResult extends NObject implements IteratorAggregate, Countabl */ public function dump() { - echo ''; - echo ''; - echo ''; - echo ''; + echo "\n
#row
\n\n\t\n\t\t\n"; + foreach ($this->getFields() as $field) { - echo ''; + echo "\t\t\n"; } - echo ''; - echo ''; - echo ''; + + echo "\t\n\n\n"; foreach ($this as $row => $fields) { - echo ''; + echo "\t\n\t\t\n"; foreach ($fields as $field) { //if (is_object($field)) $field = $field->__toString(); - echo ''; + echo "\t\t\n"; } - echo ''; + echo "\t\n"; } - echo ''; - echo '
#row' . htmlSpecialChars($field) . '" . htmlSpecialChars($field) . "
', $row, '
", $row, "', htmlSpecialChars($field), '", htmlSpecialChars($field), "
'; + echo "\n\n"; } diff --git a/dibi/libs/NObject.php b/dibi/libs/NObject.php index 0fa5ad6e..0122a82f 100644 --- a/dibi/libs/NObject.php +++ b/dibi/libs/NObject.php @@ -76,23 +76,11 @@ abstract class NObject /** * Access to reflection * - * @return ReflectionClass + * @return ReflectionObject */ final public function getReflection() { - return new ReflectionClass(get_class($this)); - } - - - - /** - * Return hash id for given object - * - * @return string 32 hexa chars - */ - final public function getHashId() - { - return spl_object_hash($this); + return new ReflectionObject($this); } @@ -217,7 +205,7 @@ abstract class NObject /** - * Is method defined? Case sensitive, filters protected & private, doesn't recognize static methods (works good since 5.0.4) + * Does method exist? Case sensitive, filters protected & private, doesn't recognize static methods (works good since 5.0.4) * * @param string class name * @param string method name @@ -236,4 +224,18 @@ abstract class NObject +/** + * NClass is the ultimate ancestor of all uninstantiable classes. + */ +abstract class NClass +{ + + final public function __construct() + { + throw new LogicException("Cannot instantiate static class " . get_class($this)); + } + } + + +} \ No newline at end of file diff --git a/examples/connect.php b/examples/connect.php index 88eec70b..00d273be 100644 --- a/examples/connect.php +++ b/examples/connect.php @@ -1,36 +1,42 @@ -

dibi connect example

+

dibi::connect() example

Connecting to Sqlite: '; try { dibi::connect(array( 'driver' => 'sqlite', 'database' => 'sample.sdb', )); - echo '

Connected to Sqlite

'; + echo 'OK'; } catch (DibiException $e) { - echo '
', $e, '
'; + echo get_class($e), ': ', $e->getMessage(); } +echo "

\n"; + // connects to MySQL using DSN +echo '

Connecting to MySQL: '; try { dibi::connect('driver=mysql&host=localhost&username=root&password=xxx&database=test&charset=utf8'); - echo '

Connected to MySQL

'; + echo 'OK'; } catch (DibiException $e) { - echo '
', $e, '
'; + echo get_class($e), ': ', $e->getMessage(); } +echo "

\n"; // connects to MySQLi using array +echo '

Connecting to MySQL: '; try { dibi::connect(array( 'driver' => 'mysqli', @@ -40,16 +46,18 @@ try { 'database' => 'dibi', 'charset' => 'utf8', )); - echo '

Connected to MySQL

'; + echo 'OK'; } catch (DibiException $e) { - echo '
', $e, '
'; + echo get_class($e), ': ', $e->getMessage(); } +echo "

\n"; // connects to ODBC +echo '

Connecting to ODBC: '; try { dibi::connect(array( 'driver' => 'odbc', @@ -57,46 +65,52 @@ try { 'password' => '***', 'database' => 'Driver={Microsoft Access Driver (*.mdb)};Dbq='.dirname(__FILE__).'/sample.mdb', )); - echo '

Connected to ODBC

'; + echo 'OK'; } catch (DibiException $e) { - echo '
', $e, '
'; + echo get_class($e), ': ', $e->getMessage(); } +echo "

\n"; // connects to PostgreSql +echo '

Connecting to PostgreSql: '; try { dibi::connect(array( 'driver' => 'postgre', 'string' => 'host=localhost port=5432 dbname=mary', 'persistent' => TRUE, )); - echo '

Connected to PostgreSql

'; + echo 'OK'; } catch (DibiException $e) { - echo '
', $e, '
'; + echo get_class($e), ': ', $e->getMessage(); } +echo "

\n"; // connects to PDO +echo '

Connecting to Sqlite via PDO: '; try { dibi::connect(array( 'driver' => 'pdo', 'dsn' => 'sqlite2::memory:', )); - echo '

Connected to Sqlite via PDO

'; + echo 'OK'; } catch (DibiException $e) { - echo '
', $e, '
'; + echo get_class($e), ': ', $e->getMessage(); } +echo "

\n"; // connects to MS SQL +echo '

Connecting to MS SQL: '; try { dibi::connect(array( 'driver' => 'mssql', @@ -104,15 +118,17 @@ try { 'username' => 'root', 'password' => 'xxx', )); - echo '

Connected to MS SQL

'; + echo 'OK'; } catch (DibiException $e) { - echo '
', $e, '
'; + echo get_class($e), ': ', $e->getMessage(); } +echo "

\n"; // connects to Oracle +echo '

Connecting to Oracle: '; try { dibi::connect(array( 'driver' => 'oracle', @@ -120,8 +136,9 @@ try { 'password' => 'xxx', 'database' => 'db', )); - echo '

Connected to Oracle

'; + echo 'OK'; } catch (DibiException $e) { - echo '
', $e, '
'; + echo get_class($e), ': ', $e->getMessage(); } +echo "

\n"; \ No newline at end of file diff --git a/examples/date.type.demo.php b/examples/date.type.demo.php index 2a54f8ee..a8afd2e8 100644 --- a/examples/date.type.demo.php +++ b/examples/date.type.demo.php @@ -1,4 +1,4 @@ -

dibi user datatype example

+

DibiVariableInterface example

Dibi Exception:'; - echo '
', $e, '
'; + echo '

', get_class($e), ': ', $e->getMessage(), '

'; }