SOLVED! HELP: php class destroying

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
itimeheke
Forum Newbie
Posts: 5
Joined: Mon Sep 27, 2010 5:10 am

SOLVED! HELP: php class destroying

Post by itimeheke »

Hi all. I need to destroy(destruct) class inside itself.
Lets say i have construct method like so:

Code: Select all

public function __construct($host = NULL, $user = NULL, $pass = NULL, $debug = false) {
	        if (!$host OR !$user) {
			// THIS IS THE PART I CAN'T FIGURE OUT
                }
		$this->debug = (bool) $debug;
		return $this->connect($host, $user, $pass);
	}
So, if variables $host or $user are not given, then i need to destroy this class. And also set the variable assigned to class as NULL.
Example:

Code: Select all

$test = new mysql();
Cause variables $host and $user aren't given, variable $test must be NULL, not an object.

Is that possible?

Regards, Kristian.
Last edited by itimeheke on Mon Sep 27, 2010 9:18 am, edited 1 time in total.
User avatar
greyhoundcode
Forum Regular
Posts: 613
Joined: Mon Feb 11, 2008 4:22 am

Re: HELP: php class destroying

Post by greyhoundcode »

How about throwing an exception from within the constructor (if it doesn't like the arguments it's been passed)?
itimeheke
Forum Newbie
Posts: 5
Joined: Mon Sep 27, 2010 5:10 am

Re: HELP: php class destroying

Post by itimeheke »

Okay, i did not explain well. For this last example, i need to set variable $test to NULL or FALSE from class 'mysql' constructor without (error)message displayed and script execution halted.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: HELP: php class destroying

Post by requinix »

You explained yourself just fine.

What you want to do cannot be done. The closest thing for a constructor is to throw an exception. Just like greyhoundcode said.
The alternative is to not use the constructor and try a factory pattern instead.
User avatar
DigitalMind
Forum Contributor
Posts: 152
Joined: Mon Sep 27, 2010 2:27 am
Location: Ukraine, Kharkov

Re: HELP: php class destroying

Post by DigitalMind »

Maybe my example will be suitable for you...

Code: Select all

class DBConn {
    private static $db = NULL;
    static function conn($host = NULL, $user = NULL, $pass = NULL, $name = NULL, $port = 3306, $debug = false) {
        if (self::$db !== NULL) {
            return self::$db;
        }
        self::$db = new mysqli($host, $user, $pass, $name, $port);
        return self::$db;
    }
    private function  __construct() {
    }
    private function  __clone() {
    }
}

$dbconn1 = DBConn::conn('localhost', 'testuser', 'testpass', 'testdb');
$dbconn2 = DBConn::conn();
$dbconn3 = DBConn::conn();
$dbconn1, $dbconn2 and $dbconn point to the same mysql connection. It's also possible to use something like $stmt = DBConn::conn()->prepare('select name from test where id = ?').
Last edited by DigitalMind on Mon Sep 27, 2010 9:29 am, edited 1 time in total.
itimeheke
Forum Newbie
Posts: 5
Joined: Mon Sep 27, 2010 5:10 am

Re: HELP: php class destroying

Post by itimeheke »

Okay, thanks. Now i know:)

digitalmind:
Yeah, my connecting method is exactly the same:) It returns mysql resource, when it exists.

Thanks for helping ya all!
Post Reply