php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #29973 Comparing COM object variable with NULL throws an exception
Submitted: 2004-09-03 19:08 UTC Modified: 2015-04-07 16:44 UTC
From: tetikr at spytech dot cz Assigned: cmb (profile)
Status: Closed Package: COM related
PHP Version: 5.* OS: Win2003
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: tetikr at spytech dot cz
New email:
PHP Version: OS:

 

 [2004-09-03 19:08 UTC] tetikr at spytech dot cz
Description:
------------
Comparing COM object variable with NULL throws an exception

Reproduce code:
---------------
$a = new COM(.....);

if (!$a)
{
   ......
}
else
{
   .....
}

Expected result:
----------------
If $a is non null, I expect the ELSE block to be performed. 

Actual result:
--------------
PHP throws an exception when trying to evaluate !$a. I think it tries to access the default COM object's property. 

If this is a valid behavior, how should I test for null?

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2004-09-06 08:06 UTC] derick@php.net
What is the full error message?
 [2004-09-06 14:27 UTC] tetikr at spytech dot cz
Some objects work and some not. The following code creates an object that works not. It throws a COM exception "Member not found". Test it for yourself, I hope it will fail :-)
---------------------------------
$o = new COM("WScript.Shell");
if (!$o) 
  /* dummy */ ;
else 
  echo $o->CurrentDirectory;
---------------------------------
 [2004-09-09 21:25 UTC] wez@php.net
in this case you should be doing:

try {
  $a = new COM(...);
  // use it here
} catch (exception $e) {
  // failed to create it
}

in other cases, where you have been passed the object, use is_object() to check if it is valid.

I'll see if I can fix the shorthand "if (!$a)" syntax someone in the next month.

 [2005-03-25 09:28 UTC] tetikr at spytech dot cz
The problem is still there. Testing a COM object created by the new operator or return by a COM property/method on NULL throws an exception "Member not found.". 

Using the is_object() function helps, but it is inconvenient and ugly. Let's make PHP better :-)
 [2010-12-20 14:04 UTC] jani@php.net
-Package: Feature/Change Request +Package: COM related
 [2013-12-13 07:44 UTC] wez@php.net
-Status: Assigned +Status: Open -Assigned To: wez +Assigned To:
 [2015-04-07 16:44 UTC] cmb@php.net
-Status: Open +Status: Closed -Assigned To: +Assigned To: cmb
 [2015-04-07 16:44 UTC] cmb@php.net
Apparently, this bug has been fixed in the meantime. I just tested
the script given in the second comment under PHP 5.4.19 and PHP
5.6.3, and it works as fine.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Apr 18 16:01:29 2024 UTC