为了账号安全,请及时绑定邮箱和手机立即绑定

PHPUnit OOP 声明

PHPUnit OOP 声明

PHP
MM们 2023-05-12 15:56:23
我试图了解我正在清理的代码的 OOP 和继承。我有一个配置文件配置.php<?php$odb_host = "localhost";$odb_name = "Prod";$odb_user = "admin";$odb_pass = "password";?>主.phpclass 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";?>
查看完整描述

1 回答

?
慕勒3428872

TA贡献1848条经验 获得超6个赞

您不应将该类传递给 oci_parse 函数。它需要一个连接资源。您可以通过调用获取资源oci_connect。在您的班级中,您的函数已经在执行此操作,因此您可以在函数中返回它。见下文。


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"; 

        }


        return $conn; // you need to return the connection.

    }

}

$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


查看完整回答
反对 回复 2023-05-12
  • 1 回答
  • 0 关注
  • 78 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信