Bug #72925 [NEW]: sqlsrv_fetch_object calls custom class constructors in static context
| From: | zburke at hshsl dot umaryland dot edu | Date: | Mon, 22 Aug 2016 20:29:33 +0000 |
| Subject: | Bug #72925 [NEW]: sqlsrv_fetch_object calls custom class constructors in static context | ||
| Groups: | php.bugs | ||
| Request: | Send a blank email to php-bugs+get-203500@lists.php.net to get a copy of this message | ||
From: zburke at hshsl dot umaryland dot edu
Operating system: Windows Server 2012 R2
PHP version: 7.0.10
Package: MSSQL related
Bug Type: Bug
Bug description:sqlsrv_fetch_object calls custom class constructors in static context
Description:
------------
When a custom classname is passed to sqlsrv_fetch_object, the
constructor is called in a static context, causing an error if it does
anything with $this. This is inconsistent with the context used by PDO
and Mysqli drivers. The problem occurs with driver version SQLSRV40.EXE
under IIS 8.5.
If you stick a print_r(debug_backtrace()) in the __construct() method of
the custom class, you can see its type is '::' with sqlsrv_fetch_object
instead of '->' for PDO->fetchObject or mysqli_fetch_object.
Test script:
---------------
<?php
class Foo
{
protected $foo = null;
public function __construct() {
$this->foo = "bar";
}
}
$conn = sqlsrv_connect( 'HOSTNAME', ["Database"=> 'DATABASE',
"UID"=>
'USERNAME', "PWD"=>'PASSWORD']);
$stmt = sqlsrv_query($conn, "SELECT * FROM SOME_TABLE");
try {
while ($obj = sqlsrv_fetch_object($stmt, 'Foo'))
;
}
catch (Error $e) {
print_r($e);
}
Expected result:
----------------
There should be no output.
Actual result:
--------------
Error Object ( [message:protected] => Using $this when not in object
context [string:Error:private] => [code:protected] => 0 [file:protected]
=> W:\path\to\wwwroot\test\db.php [line:protected] => 6
[trace:Error:private] => Array ( [0] => Array ( [function] =>
__construct [class] => foo [type] => :: [args] => Array ( ) ) [1] =>
Array ( [file] => W:\path\to\wwwroot\test\db.php [line] => 12 [function]
=> sqlsrv_fetch_object [args] => Array ( [0] => Resource id #3 [1] =>
foo ) ) ) [previous:Error:private] => )
--
Edit bug report at https://bugs.php.net/bug.php?id=72925&edit=1
--
Try a snapshot (PHP 5.4): https://bugs.php.net/fix.php?id=72925&r=trysnapshot54
Try a snapshot (PHP 5.5): https://bugs.php.net/fix.php?id=72925&r=trysnapshot55
Try a snapshot (trunk): https://bugs.php.net/fix.php?id=72925&r=trysnapshottrunk
Fixed in SVN: https://bugs.php.net/fix.php?id=72925&r=fixed
Fixed in release: https://bugs.php.net/fix.php?id=72925&r=alreadyfixed
Need backtrace: https://bugs.php.net/fix.php?id=72925&r=needtrace
Need Reproduce Script: https://bugs.php.net/fix.php?id=72925&r=needscript
Try newer version: https://bugs.php.net/fix.php?id=72925&r=oldversion
Not developer issue: https://bugs.php.net/fix.php?id=72925&r=support
Expected behavior: https://bugs.php.net/fix.php?id=72925&r=notwrong
Not enough info: https://bugs.php.net/fix.php?id=72925&r=notenoughinfo
Submitted twice: https://bugs.php.net/fix.php?id=72925&r=submittedtwice
register_globals: https://bugs.php.net/fix.php?id=72925&r=globals
PHP 4 support discontinued: https://bugs.php.net/fix.php?id=72925&r=php4
Daylight Savings: https://bugs.php.net/fix.php?id=72925&r=dst
IIS Stability: https://bugs.php.net/fix.php?id=72925&r=isapi
Install GNU Sed: https://bugs.php.net/fix.php?id=72925&r=gnused
Floating point limitations: https://bugs.php.net/fix.php?id=72925&r=float
No Zend Extensions: https://bugs.php.net/fix.php?id=72925&r=nozend
MySQL Configuration Error: https://bugs.php.net/fix.php?id=72925&r=mysqlcfg