Jump to content

Archived

This topic is now archived and is closed to further replies.

crazyone

Php 5 Objects And Resources

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]

Share this post


Link to post
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.

Share this post


Link to post
Share on other sites
[!--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

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites

×

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.