Problem with resources and php 5 objects

From: Date: Wed, 28 Jun 2006 17:55:12 +0000
Subject: Problem with resources and php 5 objects
Groups: php.general 
Request: Send a blank email to php-general+get-238819@lists.php.net to get a copy of this message
I created a php class in charge of doing mysql queries and when this query is called it returns an object of the type "myddl_result", this second object is passed the resource of type mysql_result and if i debug the whole thing i seems allright until the __construct is complete, after that, the __destruct is called for i don't know which reason and the rest is executed normally.... At that point my resource is of type unknown and can't be used anymore... I really need help on that please... i don't understand whats happening even after reading the php website 2x about php 5 objects... Note: What i understand is that the __destruct is called and thats what destroys my resource since i call the mysql_free_result, i tried commenting it but obviously thats not what i want, i want to free the resource when the object is destroyed which is good pratice, but WHY is the __destruct called, it doesn't make sense it's not supposed to be destroyed it's just been created... ====================================================== QUERY FUNCTION ====================================================== public function query($sql, $buffered = true){ if($this->p_mylink){ //execute the query and look for an exception if($buffered == true){ $res = mysql_query($sql, $this->p_mylink); }else{ $res = mysql_unbuffered_query($sql, $this->p_mylink); }
  if(is_resource($res)){    //Result returned
$mddl_result = new myDDLResult($res); echo 'Completed creation<br><pre>'; var_dump($mddl_result); echo '</pre><br>';
   return $mddl_result;    //Create a result object and return it
  }elseif($res === true){    //Command executed succesfully
   return new myDDLResult();    //Create a result object and return it
  }elseif($res === false){    //Can only mean an error occured
   return $this->getError();    //Create a result object and return it
  }else{    //Impossible case but we'll warn the user
throw new myDDLException('query failed to compare mysql_query type'); } } throw new myDDLInvalidOperationException('Not connected to server'); } ========================================================= RESULT CLASS ========================================================= //Class in charge of interfacing a result final class myDDLResult {
    //Local vars
    private $p_myresult;
    //Constructor / destructor
    public function __construct($result = NULL){
        //Store
        echo 'Creating';
        echo '<br>';
        var_dump($result);
        echo '<br>';
        $this->p_myresult = $result;
    }
    public function __destruct(){
        echo 'Destroying';
        echo '<br>';
        var_dump($this->p_myresult);
        echo '<br>';
        mysql_free_result($this->p_myresult);
    }
} ==================================================== OUTPUT ==================================================== Creating resource(3) of type (mysql result) Destroying resource(3) of type (mysql result) Completed creation object(myDDLResult)#1 (1) { ["p_myresult:private"]=> resource(3) of type (Unknown) } Destroyingresource(3) of type (Unknown) Warning: mysql_free_result(): 3 is not a valid MySQL result resource in C:\Source\palliscience\maj.palliscience.com\dev\lib_myddl.php on line 200 Destroying resource(3) of type (Unknown) Warning: mysql_free_result(): 3 is not a valid MySQL result resource in C:\Source\palliscience\maj.palliscience.com\dev\lib_myddl.php on line 200 Destroying resource(3) of type (Unknown) Warning: mysql_free_result(): 3 is not a valid MySQL result resource in C:\Source\palliscience\maj.palliscience.com\dev\lib_myddl.php on line 200 Destroying resource(3) of type (Unknown)

« previous php.general (#238819) next »