Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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;
    }     
}
?>
share|improve this question
    
$secArray->secArray['from_date1'] and so on. –  Marc B May 6 '12 at 22:59

1 Answer 1

up vote 0 down vote accepted

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.

share|improve this answer
    
Thank you Marc and RADU for your great assistance –  yhammad May 7 '12 at 19:13

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.