我是面向對象PHP概念的新手(在www.killerphp.com上的教程之後),我打算將所有的php應用程序遷移到OO PHP。
我開始根據「where」條件設置的方法「setSecurity()」基於對象屬性從數據庫中讀取授權級別。面向對象的PHP返回數據庫行和訪問數組元素
我設法返回數組「getSecurity()」,但打印輸出將給:
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元素()方法(從Array 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;
}
}
?>
'$ secArray-> secArray ['from_date1']'等等。 –