PHP中如何定义对象类型数组
例如:第一个文件:文件名为:Department.php<?phpclassDepartment{private$department;publicfunctionget...
例如 :
第一个文件:文件名为:Department.php
<?php
class Department
{
private $department;
public function getDepartment()
{
return $this->department;
}
public function setDepartment($department)
{
$this->department=$department;
}
}
?>
第二个文件:
<?php
define('ROOT_PATH',dirname(dirname(__FILE__)));
include (ROOT_PATH."/Common/class.DBConnect.php");
include (ROOT_PATH."/Class/class.Department.php");
class Select
{
public function selectDepartment()
{
$sql="select * from YX_Department";
$DBObject= new DBConnect();
$result=$DBObject->DBControl($sql);
$DepartObject= new Department();
while ($row=mysql_fetch_array($result))
{
$DepartObject->setDepartment($row[0]);
}
}
}
?>
我想把数据库里面读出来的数据循环存储到 Department的对象数组中,方便再其他地方调用,请问while里面应该怎么写呢。 展开
第一个文件:文件名为:Department.php
<?php
class Department
{
private $department;
public function getDepartment()
{
return $this->department;
}
public function setDepartment($department)
{
$this->department=$department;
}
}
?>
第二个文件:
<?php
define('ROOT_PATH',dirname(dirname(__FILE__)));
include (ROOT_PATH."/Common/class.DBConnect.php");
include (ROOT_PATH."/Class/class.Department.php");
class Select
{
public function selectDepartment()
{
$sql="select * from YX_Department";
$DBObject= new DBConnect();
$result=$DBObject->DBControl($sql);
$DepartObject= new Department();
while ($row=mysql_fetch_array($result))
{
$DepartObject->setDepartment($row[0]);
}
}
}
?>
我想把数据库里面读出来的数据循环存储到 Department的对象数组中,方便再其他地方调用,请问while里面应该怎么写呢。 展开
2个回答
展开全部
应该是存到对象属性中吧
你把
public function setDepartment($department)
{
$this->department=$department;
}
改成
public function setDepartment($department)
{
$this->department[]=$department;
}
即可
private $department;
要不要改成
private $department=array();
其实无所谓
你把
public function setDepartment($department)
{
$this->department=$department;
}
改成
public function setDepartment($department)
{
$this->department[]=$department;
}
即可
private $department;
要不要改成
private $department=array();
其实无所谓
更多追问追答
追问
那获取值的时候该怎么写呢。如何判断数组长度呀。。
追答
获取值你用同一个$DepartObject对象(注意,我说的是同一个对象,不明白继续问我)
调用getDepartment这个方法(这些代码是你自己写的么?)
$DepartObject->getDepartment();
照你这样的写法
估计得用全局变量
$GLOBALS["DepartObject"]->getDepartment();
这样的写法才能在其他的地方使用。。
数组长度用count,不知道怎么用去查php使用手册吧
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
$a=array();
while ($row=mysql_fetch_array($result))
{
$d= new Department();
$d->setDepartment($row[0]);
$a[]=$d;
}
while ($row=mysql_fetch_array($result))
{
$d= new Department();
$d->setDepartment($row[0]);
$a[]=$d;
}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询