关于foreache()的问题; 这里foreach($users as $user)为什么要as $user呢?$user哪里来的.
<?phpabstractclassUser{protected$name=NULL;functionUser($name){$this->name=$name;}fun...
<?php
abstract class User
{
protected $name=NULL;
function User($name)
{
$this->name=$name;
}
function getName()
{
return $this->name;
}
function viewAccess()
{
return "No";
}
function editAccess()
{
return "No";
}
function delectAccess()
{
return "No";
}
function manageAccess()
{
return "No";
}
}
class Client extends User
{
function viewAccess()
{
return "Yes";
}
}
class Administrator extends User
{
function viewAccess()
{
return "Yes";
}
function editAccess()
{
return "Yes";
}
function delectAccess()
{
return "Yes";
}
}
class Supporter extends User
{
function viewAccess()
{
return "Yes";
}
function editAccess()
{
return "Yes";
}
function delectAccess()
{
return "Yes";
}
function manageAccess()
{
return "Yes";
}
}
class Factory
{
private static $users=array("Simon"=>"Client","Elaine"=>"Administrator","Bob"=>"Supporter");
static function Create($name)
{
switch(self::$users[$name])
{
case "Client";
return new Client($name);
case "Administrator";
return new Administrator($name);
case "Supporter";
return new Supporter($name);
}
}
}
$users=array("Elaine","Simon","Bob");
foreach($users as $user)
{
$obj = Factory::Create($user);
echo $obj->getName()."的权限:<BR>";
echo "浏览:".$obj->viewAccess()."<BR>";
echo "修改:".$obj->editAccess()."<BR>";
echo "删除:".$obj->delectAccess()."<BR>";
echo "管理:".$obj->manageAccess()."<BR>";
}
?> 展开
abstract class User
{
protected $name=NULL;
function User($name)
{
$this->name=$name;
}
function getName()
{
return $this->name;
}
function viewAccess()
{
return "No";
}
function editAccess()
{
return "No";
}
function delectAccess()
{
return "No";
}
function manageAccess()
{
return "No";
}
}
class Client extends User
{
function viewAccess()
{
return "Yes";
}
}
class Administrator extends User
{
function viewAccess()
{
return "Yes";
}
function editAccess()
{
return "Yes";
}
function delectAccess()
{
return "Yes";
}
}
class Supporter extends User
{
function viewAccess()
{
return "Yes";
}
function editAccess()
{
return "Yes";
}
function delectAccess()
{
return "Yes";
}
function manageAccess()
{
return "Yes";
}
}
class Factory
{
private static $users=array("Simon"=>"Client","Elaine"=>"Administrator","Bob"=>"Supporter");
static function Create($name)
{
switch(self::$users[$name])
{
case "Client";
return new Client($name);
case "Administrator";
return new Administrator($name);
case "Supporter";
return new Supporter($name);
}
}
}
$users=array("Elaine","Simon","Bob");
foreach($users as $user)
{
$obj = Factory::Create($user);
echo $obj->getName()."的权限:<BR>";
echo "浏览:".$obj->viewAccess()."<BR>";
echo "修改:".$obj->editAccess()."<BR>";
echo "删除:".$obj->delectAccess()."<BR>";
echo "管理:".$obj->manageAccess()."<BR>";
}
?> 展开
1个回答
展开全部
$user是$users数组里的一个单元,是在此循环中新产生的变量。
foreach完整写法是foreach($array as $k=>$v)
$k是下标,$v是数组里的每个值
举个例子:
$peoples=array('xm'=>'name','xb'=>'sex','mz'=>'nation','cs'=>'birth');
foreach($peoples as $k=>$v)
{
echo $k.'-----'.$v.'<br>';
}
结果是
xm-----name
xb-----sex
mz-----nation
cs-----birth
foreach完整写法是foreach($array as $k=>$v)
$k是下标,$v是数组里的每个值
举个例子:
$peoples=array('xm'=>'name','xb'=>'sex','mz'=>'nation','cs'=>'birth');
foreach($peoples as $k=>$v)
{
echo $k.'-----'.$v.'<br>';
}
结果是
xm-----name
xb-----sex
mz-----nation
cs-----birth
追问
我没理解的是为什么这里是$user而不是其他的比如$key 我把这里的$user换成其他的比如$key 或者$name都是错的 为什么偏偏是$user呢?在其他地方也没定义过这个$user啊。
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询