Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sqlsrv_fetch_object calls custom class constructors in static context #139

Closed
zburke opened this issue Aug 23, 2016 · 2 comments
Closed

Comments

@zburke
Copy link

zburke commented Aug 23, 2016

Description:

When a custom classname is passed to sqlsrv_fetch_object, the constructor is called in a static context, causing an error if the constructors attempts to manipulate $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] => 
)
@meet-bhagdev
Copy link
Contributor

@zburke We are actively looking into this. Thank you for the detailed repro script.

@Hadis-Knj
Copy link

@zburke This is fixed in the Windows 4.1.3 release and Linux 4.0.5 release. I'll close the issue, please let us know if you faced other issues.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants