簡體   English   中英

面向對象的PHP返回數據庫行和訪問數組元素

[英]Object Oriented PHP return database row and access array elements

我是面向對象的PHP概念的新手(遵循www.killerphp.com上的教程),我正計划將所有的php應用程序遷移到OO PHP。
condition set by method . 我開始構造第一個類,該類基於方法設置的條件下的對象屬性,從數據庫中讀取授權級別。

but printing the output would give: 我設法返回數組但打印輸出將得到:

security Object
(
    [secArray] => Array
        (
            [from_date1] => 1992-01-01
            [to_date1] => 0000-00-00
            [from_date2] => 1992-01-01
            [to_date2] => 0000-00-00
            [view] => 1
            [insert] => 0
            [update] => 1
            [delete] => 1
            [valid] => 1
        )

)
/*"Array 1"*/

我的問題是我不熟悉普通數組(如下)的打印輸出。

Array
(
    [from_date1] => 1992-01-01
    [to_date1] => 0000-00-00
    [from_date2] => 1992-01-01
    [to_date2] => 0000-00-00
    [view] => 1
    [insert] => 0
    [update] => 1
    [delete] => 1
    [valid] => 1
)
/*"Array 2"*/

我的問題是:1)如何從getSecurity()方法(從數組1)訪問數組的元素?
2)如何獲取正確返回數組的方法(與數組2相同)?

可以在下面找到代碼段。

非常感謝您的支持...

'test.php'

<?php
include("connect.php");  
include("security.php");
$secArray=new security();
$secArray->setSecurity('test_user',1,1,1,$link);
$secArray->getSecurity();
echo "<pre>"; print_r($secArray); echo "</pre>";
?>

'security.php'

<?php
class security
{
    public $secArray = array();

    function setSecurity($user,$appid,$funid,$objid,$conn='')
    {
        $query="SELECT lu.DATE1 as from_date1,
                    lu.DATE2 as to_date1,
                    ga.DATE1 as from_date2,
                    ga.DATE2 as to_date2,
                    ga.VIEW as view,
                    ga.INSERT as insert,
                    ga.UPDATE as update,
                    ga.DELETE as delete,
                    ob.VALID as valid
                FROM
                    user as lu
                    inner join group as ug on lu.GRP_ID = ug.ID
                    inner join privileges as ga on lu.GRP_ID = ga.GRP_ID
                    and ug.ID = ga.GRP_ID
                    inner join level1 as ob on ob.APP_ID = ga.APP_ID
                    and ob.FUN_ID = ga.FUN_ID
                    and ob.ID = ga.OBJ_ID
                where
                    USERID = '$user'
                and ga.APP_ID = $appid
                and ga.FUN_ID = $funid
                and ga.OBJ_ID = $objid";
        $result = mysql_query($query,$conn);
        $row = mysql_fetch_assoc($result);
        $this->secArray=$row;
    }

    function getSecurity()
    {
        return $this->secArray;
    }     
}
?>

該getter返回一個值,因此調用$secArray->getSecurity(); 除非您對返回的值進行某些操作,否則不會真正幫助您。 $mySecurity=$secArray->getSecurity(); use $mySecurity...

請閱讀有關訪問數組和對象的PHP文檔。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM