Jump to content

Recommended Posts

I created a set of class to manage mysql server structure and part of this has two classes used for query and result management. I got a problem storing the result in one of my classes because of the following :

It seems PHP is calling the constructor (__construct) then immediatly calling the destructor for no apparent reason... (__destruct). Because of that i can't pass my resource to my constructor because the destructor called immedialty after destroys it. This either forces me to set the result AFTER the creation of the object which is not normal IMO.

Is this a normal behavior of PHP5, a bug in the windows binary? I'm running PHP 5.1.2 on a windows 2003 server (Which i doubt is a problem in this case)

Here is some code

[code]======================================================
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) [/code]
Link to comment
https://forums.phpfreaks.com/topic/13136-php-5-objects-and-resources/
Share on other sites

Could it be because you're using vardump? Or was this happening before you tried vardump? Also there are several very robust PEAR packages that handle DB abstraction and I strongly suggest you use one of them unless this is a learning experience for you. There really isn't any reason to reinvent the wheel.
[!--quoteo(post=388939:date=Jun 28 2006, 02:43 PM:name=Buyocat)--][div class=\'quotetop\']QUOTE(Buyocat @ Jun 28 2006, 02:43 PM) [snapback]388939[/snapback][/div][div class=\'quotemain\'][!--quotec--]
Could it be because you're using vardump? Or was this happening before you tried vardump? Also there are several very robust PEAR packages that handle DB abstraction and I strongly suggest you use one of them unless this is a learning experience for you. There really isn't any reason to reinvent the wheel.
[/quote]

Nope, it happens on several levels, not just this one, if i put a $this->connect() in my server connection class it also loses my link resource...

And don't worry, i'm not reinventing the wheel, i am doing a complete data definition language abstraction layer that doesn't already exist on PEAR. Or else i didn't look correctly last time i went there. Overall i can't find any php5 compatible DDL and DML abstraction library.

Back to my problem, what i don't understand is why the __destruct is being called for nothing, i'll try some other tests, can you keep me updated on stuff you try thnks
For all to know

My previous question was in fact a real PHP bug. This bug is linux AND windows, the problem is caused when you have the

zend.ze1_compatibility_mode = On

in the php.ini file. This is a bug that was reported before several times without having been resolved. I commented and reactiveated the bug on the php bug submission engine...

Math
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.