我是面向对象PHP概念的新手(遵循www.killerphp.com上的教程),我计划将我的所有php应用程序迁移到OO PHP。 我开始构建我的第一个类,它根据方法“setSecurity()”设置的“where”条件中的对象属性从数据库中读取授权级别。
我设法返回数组“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()方法(从数组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;
}
}
?>
答案 0 :(得分:0)
getter返回一个值,因此调用$secArray->getSecurity();
并不能真正帮助你,除非你对返回的值做了些什么。 $mySecurity=$secArray->getSecurity();
use $mySecurity...
请阅读有关访问数组和对象的PHP文档。