简体   繁体   中英

Object Oriented PHP return database row and access array elements

I am new to the concept of Object Oriented PHP (following the tutorial on www.killerphp.com) and I am planning to migrate all my php applications to OO PHP.
I started constructing my first class which reads authorization levels from database based on object properties in the "where" condition set by method "setSecurity()" .

I managed to return the array "getSecurity()" 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"*/

My problem is that I am not familiar with the printed output to that of a normal array (below).

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"*/

My questions are: 1) How can I access the elements of my array from getSecurity() method (from Array 1)?
2) How can I get my method to return the array properly (same as Array 2)?

Code Snippets found below.

Thank you very much for any support...

'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;
    }     
}
?>

The getter returns a value so calling $secArray->getSecurity(); doesn't really help you unless you do something with the value returned. $mySecurity=$secArray->getSecurity(); use $mySecurity...

Please read PHP documentation on accessing arrays and objects.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM