nekokumi Posted January 14, 2008 Share Posted January 14, 2008 Hi all I have a curl http class that i created, basically here is the constructor and destrcutor class CurlHTTPClient { private $ch; public function __construct() { $this->ch = curl_init(); } public function __destruct() { curl_close($this->ch); } public function get($url, $referer = '') { return 'the url data'; } then i write thing like this, for example session_start(); // Because i'm planning to use session $a = new CurlHTTPClient(); echo $a->get('www.google.com'); It told me there is a warning (which is i think the problem in __destruct) described below Warning: curl_close(): supplied argument is not a valid cURL handle If i remove session_start(), it works nicely. How do i solve this problem? or suggestion. (Since i'm relatively new to php, but not to programming) Thank you Quote Link to comment https://forums.phpfreaks.com/topic/86036-solved-destructor-problem-with-session_start/ Share on other sites More sharing options...
toplay Posted January 15, 2008 Share Posted January 15, 2008 I'm not sure why off hand, but it's not that important since it's a warning on a close and you can suppress the error by doing: @curl_close($this->ch); Quote Link to comment https://forums.phpfreaks.com/topic/86036-solved-destructor-problem-with-session_start/#findComment-439365 Share on other sites More sharing options...
teng84 Posted January 15, 2008 Share Posted January 15, 2008 i dont think putting that suppressor is the solution because it says invalid argument so it will not show error and it will not work Quote Link to comment https://forums.phpfreaks.com/topic/86036-solved-destructor-problem-with-session_start/#findComment-439369 Share on other sites More sharing options...
toplay Posted January 15, 2008 Share Posted January 15, 2008 i dont think putting that suppressor is the solution because it says invalid argument so it will not show error and it will not work It's not invalid argument but an invalid resource that's passed. It doesn't matter because the resource has probably already been cleared out (closed) by the time the actual call to curl_close() in the destructor has taken place. If that error was for the curl_init() function, then I wouldn't be recommending to suppressor anything. When there's PHP sessions involved, PHP internally does a lot of clean up, like save the session, clear memory, close open resources, etc. I suspect that the curl resource got cleaned up by PHP internally, then it called your destructor, which by that time, the resource is no longer available (and hence nothing to close and you get the warning). nekokumi, it also could be that the curl_init() never really worked in the first place. That function could return a boolean FALSE value instead of returning a valid resource. So, always check for errors and don't assume you got back what you're expecting. http://us.php.net/manual/en/function.curl-init.php Quote Link to comment https://forums.phpfreaks.com/topic/86036-solved-destructor-problem-with-session_start/#findComment-439382 Share on other sites More sharing options...
nekokumi Posted January 15, 2008 Author Share Posted January 15, 2008 thanks for your reply guys, so php session clean up internally without using the destructor, it's a bit frightening for me. i manage to remove the warning by using if if ($this->ch) curl_close($this->ch); Quote Link to comment https://forums.phpfreaks.com/topic/86036-solved-destructor-problem-with-session_start/#findComment-439543 Share on other sites More sharing options...
toplay Posted January 15, 2008 Share Posted January 15, 2008 Yes, that's fine but you could use is_resource(). Quote Link to comment https://forums.phpfreaks.com/topic/86036-solved-destructor-problem-with-session_start/#findComment-439583 Share on other sites More sharing options...
nekokumi Posted January 15, 2008 Author Share Posted January 15, 2008 Yes, that's fine but you could use is_resource(). thank you Quote Link to comment https://forums.phpfreaks.com/topic/86036-solved-destructor-problem-with-session_start/#findComment-439680 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.