473,320 Members | 1,839 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Listing all objects

code green
1,726 Expert 1GB
I like to cleanly handle the destruction of all objects on script termination.
This is very useful when developing because I can 'round up' all the errors
even when the script fails.

At the moment I am explicitly naming the objects in a shutdown function
and accessing them globally: Example
Expand|Select|Wrap|Line Numbers
  1. function shutdownFunc()
  2. {
  3.         global $mysql;
  4.     if(is_object($mysql))
  5.         disconnectDB($mysql,$errorlist);
  6.  
  7.     global $mssql;
  8.     if(is_object($mssql))
  9.         disconnectDB($mssql,$errorlist);
  10. }
This works but requires a shutdownFunc() for every project
because diiferent objects are created..
I am now working on the idea of making shutdownFunc() a top-level class::method
used by all projects but this means finding all objects dynamically.

The closest I can think is using get_defined_vars() then is_object().
Is there a better way or even a better idea?
May 29 '09 #1
1 1556
Dormilich
8,658 Expert Mod 8TB
if you're using PHP 5 you can make use of the __destruct() method.

say we're using PDO for DB connection:
Expand|Select|Wrap|Line Numbers
  1. abstract class MyDB // implements DB_connector
  2. {
  3.     /**
  4.      * @var (object) $PDO           PDO DB object
  5.      * @var (array) $PS             PDOStatement object storage array
  6.      * @var (string) $DB_NAME       DB table name
  7.      */
  8.     private static $PDO      = NULL;
  9.     private static $PS       = array();
  10.     public  static $DB_NAME  = MY_DB_NAME;
  11.  
  12.     /**
  13.      * @desc close DB connection on script end
  14.      * @return (void)
  15.      */
  16.     public function __destruct()
  17.     {
  18.         self::$PDO = NULL;
  19.     }
  20.  
  21.     /**
  22.      * @desc connect to the DB using PDO in a Singleton like pattern.
  23.      * @return (void)
  24.      * @thrown (Exception)
  25.      */
  26.     public static function connect()
  27.     {
  28.         if (self::$PDO === NULL)
  29.         {
  30.             try {
  31.                 $dsn = 'mysql:host=' . DB_SERVER . ';dbname=' . self::$DB_NAME;
  32.                 self::$PDO = new PDO($dsn, DB_USER, DB_PASS);
  33.             }
  34.             catch (PDOException $pdo)
  35.             {
  36.                 ErrorLog::logException($pdo, __METHOD__); // my error logger
  37.                 throw new Exception("Connecting to MySQL failed."); // rethrow Exception, because we can't proceed without DB
  38.             }
  39.         }
  40.     }
  41.  
  42.     // other methods coming here
  43. }
May 29 '09 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

10
by: Chris | last post by:
Hi, Not sure if this is the right forum, but hopefully someone can help me. I am creating something for our intranet and i want to list the files and folders of a directory, i found some code...
19
by: SU News Server | last post by:
I've struggled with this for quite a while and I'm am just not sure what is going on. I have the following code import os def buildList( directory='/Users/mkonrad' ) dirs = listing =...
4
by: jbesr1230 | last post by:
Hello, How would I print out a listing of the queries that are in a database? I would like to have a spreadsheet style listing with the columns of: Query Name, Description, Modified Date, Created...
3
by: David Jacques | last post by:
I am trying to get a list of all files of a certain extension type on disk to do some processing in a loop. The code needs to be portable to UNIX, so I need to use plain c functionality. Does...
8
by: Lüpher Cypher | last post by:
Hi, Suppose we have a hierarchical class structure that looks something like this: Object | +-- Main | +-- Object1
1
by: MLH | last post by:
Am having trouble with the filter property setting below. Would like to filter the listing to car makes beginning with "D". I'm blowing it on the filter spec somehow??? Sub OpenRecordsetX() ...
1
by: Johnny Jörgensen | last post by:
I've got a serious problem. I've got Visual Studio 2005 installed, and of course I'm using the Pretty Listing formatting function. When I start up VS, everything is fine, but after a while (which...
9
by: Ramashish Baranwal | last post by:
Hi, I want to get a module's contents (classes, functions and variables) in the order in which they are declared. Using dir(module) therefore doesn't work for me as it returns a list in...
5
by: jain236 | last post by:
HI every body, i am always getting the following error while parsing a directory . i am reading a directory by doing ls and trying to find out the name,type,size, mtime and mode of files from...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.