php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #72925 sqlsrv_fetch_object calls custom class constructors in static context
Submitted: 2016-08-22 20:29 UTC Modified: 2016-08-23 04:42 UTC
From: zburke at hshsl dot umaryland dot edu Assigned:
Status: Suspended Package: sqlsrv (PECL)
PHP Version: 7.0.10 OS: Windows Server 2012 R2
Private report: No CVE-ID: None
 [2016-08-22 20:29 UTC] zburke at hshsl dot umaryland dot edu
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] => )

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2016-08-23 04:42 UTC] requinix@php.net
-Status: Open +Status: Suspended -Package: MSSQL related +Package: sqlsrv
 [2016-08-23 04:42 UTC] requinix@php.net
Reported on GitHub: https://github.com/Azure/msphpsql/issues/139

More knowledgeable folks are free to correct me, but I'm going to mark this Suspended since it appears the GitHub repo is a better (preferred?) place to report bugs for SQLSRV.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Mon Dec 30 14:01:28 2024 UTC