• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

类方法中使用array_map报错— Cannot redeclare

php 搞代码 3年前 (2022-01-23) 37次浏览 已收录 0个评论
文章目录[隐藏]

方法定义如下:
class maaper{
……
public function getProperties(){
function getName($reflectionProperties){
return $reflectionProperties->name;
}
$domain=$this->get_domain();
$reflectionProperties=$domain->getProperties(ReflectionProperty::IS_PUBLIC);
$properties=array_map(‘getName’, $reflectionProperties);
return $properties;
}

……
}

调用过程如下(注:$mapper_1、$mapper_2是在同一次运行时调用方法):

$mapper_1=new mapper();
$mapper_2=new mapper();

var_dump($mapper_1->getProperties()); //正确返回
var_dump($mapper_2->getProperties()); //报错 Cannot redeclare getName()

截图如下:

回复讨论(解决方案)

你不看错误信息的吗?
Fatal error: Cannot redeclare getName()
致命错误:无法重新声明 getName()

class maaper{
protected function getName($reflectionProperties){
return $reflectionProperties->name;
}
public function getProperties(){
$domain=$this->get_domain();
$reflectionProperties=$domain->getProperties(ReflectionProperty::IS_PUBLIC);
$properties=array_map( array($this, ‘getName’), $reflectionProperties);
return $properties;
}

也可写作
class maaper{
public function getProperties(){
$getName = function($reflectionProperties){
return $reflectionProperties->name;
};
$domain=$this->get_domain();
$reflectionProperties=$domain->getProperties(ReflectionProperty::IS_PUBLIC);
$properties=array_map( $getName, $reflectionProperties);
return $properties;
(本文来源gaodai#ma#com搞@@代~&码网

搞代gaodaima码

}

你不看错误信息的吗?
Fatal error: Cannot redeclare getName()
致命错误:无法重新声明 getName()

class maaper{
protected function getName($reflectionProperties){
return $reflectionProperties->name;
}
public function getProperties(){
$domain=$this->get_domain();
$reflectionProperties=$domain->getProperties(ReflectionProperty::IS_PUBLIC);
$properties=array_map( array($this, ‘getName’), $reflectionProperties);
return $properties;
}

也可写作
class maaper{
public function getProperties(){
$getName = function($reflectionProperties){
return $reflectionProperties->name;
};
$domain=$this->get_domain();
$reflectionProperties=$domain->getProperties(ReflectionProperty::IS_PUBLIC);
$properties=array_map( $getName, $reflectionProperties);
return $properties;
}
非常感谢

但是为什么会出现重新声明的错误呢?

你有
$reflectionProperties=$domain-> getProperties(ReflectionProperty::IS_PUBLIC);
就是重新进入了 getProperties,那么在 getProperties 中定义的函数不就重复在定义吗?

你有
$reflectionProperties=$domain-> getProperties(ReflectionProperty::IS_PUBLIC);
就是重新进入了 getProperties,那么在 getProperties 中定义的函数不就重复在定义吗?

虽然有两次调用 $domain-> getProperties 但是此处的getProperties是定义在$domain领域对象所属的类中,其中并没有getName方法。 报错信息是无法重新申明位于class mapper里的位于 getProperties的getName方法。

所以。。。。好像不是这么回事吧

function getName($reflectionProperties){
return $reflectionProperties->name;
}
无论在哪里都是定义全局函数

function getName($reflectionProperties){
return $reflectionProperties->name;
}
无论在哪里都是定义全局函数
一语中的啊
谢谢!!!


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:类方法中使用array_map报错— Cannot redeclare

喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址