PHPUnit OOP 声明

我试图了解我正在清理的代码的 OOP 和继承。


我有一个配置文件


配置.php


<?php

$odb_host = "localhost";

$odb_name = "Prod";

$odb_user = "admin";

$odb_pass = "password";

?>

主.php


class upSellCore{


    public function ociConnect($odb_user,$odb_pass,$odb_host,$odb_name)

    {

        $db = "(DESCRIPTION=(ADDRESS_LIST = (ADDRESS = (PROTOCOL = TCP)(HOST = ".$odb_host.")(PORT = 1521 )))(CONNECT_DATA=(SID=".$odb_name.")))";

        $conn = oci_connect($odb_user, $odb_pass, $db);

        if (!$conn) {

        $e = oci_error();

        trigger_error(htmlentities($e['message'], ENT_QUOTES), E_USER_ERROR);

        }

        else

        {

            print "ERR01"; // For PHPUnit assertTrue

        }

    }

}

$sql = "Select * from users ";


$upSell = new upSellCore();

$upSell->ociConnect($odb_user,$odb_pass,$odb_host,$odb_name);

$stid = oci_parse($upSell,$sql); // Line having issue

因为我已经初始化调用 OciConnect 但是当我尝试传递对象以触发 oci_parse


我收到以下错误:-


PHP Warning:  oci_parse() expects parameter 1 to be resource, object given in /I/main.php on line 46 

它$conn本身是来自 Oracle 类的对象,但是当我覆盖我的对象 $upSell 时,我似乎无法将连接解析为 oci_parse。


取自 PHPManual 的端到端而不使用 OOP


<?php


$conn = oci_connect('hr', 'welcome', 'localhost/XE', 'AL32UTF8');

if (!$conn) {

    $e = oci_error();

    trigger_error(htmlentities($e['message'], ENT_QUOTES), E_USER_ERROR);

}


$stid = oci_parse($conn, 'SELECT * FROM employees');

oci_execute($stid);


echo "<table border='1'>\n";

while ($row = oci_fetch_array($stid, OCI_ASSOC+OCI_RETURN_NULLS)) {

    echo "<tr>\n";

    foreach ($row as $item) {

        echo "    <td>" . ($item !== null ? htmlentities($item, ENT_QUOTES) : "&nbsp;") . "</td>\n";

    }

    echo "</tr>\n";

}

echo "</table>\n";


?>


MM们
浏览 84回答 1
1回答

慕勒3428872

您不应将该类传递给 oci_parse 函数。它需要一个连接资源。您可以通过调用获取资源oci_connect。在您的班级中,您的函数已经在执行此操作,因此您可以在函数中返回它。见下文。class upSellCore{&nbsp;&nbsp; &nbsp; public function ociConnect($odb_user,$odb_pass,$odb_host,$odb_name)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; $db = "(DESCRIPTION=(ADDRESS_LIST = (ADDRESS = (PROTOCOL = TCP)(HOST = ".$odb_host.")(PORT = 1521 )))(CONNECT_DATA=(SID=".$odb_name.")))";&nbsp; &nbsp; &nbsp; &nbsp; $conn = oci_connect($odb_user, $odb_pass, $db);&nbsp; &nbsp; &nbsp; &nbsp; if (!$conn) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $e = oci_error();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; trigger_error(htmlentities($e['message'], ENT_QUOTES), E_USER_ERROR);&nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; print "ERR01";&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; return $conn; // you need to return the connection.&nbsp; &nbsp; }}$sql = "Select * from users ";$upSell = new upSellCore();$conn = $upSell->ociConnect($odb_user,$odb_pass,$odb_host,$odb_name); // you get the returned connection here and use it in the following line.$stid = oci_parse($conn, $sql); // this is expecting a resource
打开App,查看更多内容
随时随地看视频慕课网APP