子类上的 get_class_var
我有以下设置:
abstract class AParentLy{
private $a;
private $b;
function foo(){
foreach(get_class_vars(get_called_class()) as $name => $value){
echo "$name holds $value";
}
}
}
class ChildClass extends AParentLy{
protected $c='c';
protected $d='d';
}
$object = new ChildClass();
$object->foo();
我希望它输出的是:
c holds c
d holds d
它的输出是:
c holds c
d holds d
a holds
b holds
get_used_class() 方法正确输出“ChildClass”
我对 PHP 中的类继承相当陌生,从我可以收集到的情况来看,问题出在某处如此范围内。但我不知道如何让它发挥作用。
(有点可疑的解决方案是继续添加一个很大的 if($name!='a' && $name!='b' ...~ 到混合中。但我确信有必须是另一种更理智、更稳定的方式来做到这一点)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
再次尝试整个实验,这个问题是其中的一部分。
我最终的解决方案(以防万一有人遇到这个问题并遇到同样的问题)是在父类中创建以下方法:
通过这个方法,您可以获得子类的每个参数(及其值),而无需任何父类。如果您更改类名,则必须稍微更改该函数,但至少对我来说这是一个可行的解决方案。
Had another go at the whole experiment this question was a part of.
The eventual solution (just in case anyone stumbles over this and has the same problem) I came to was to create the following method within the parent class:
with this you get every parameter (and their value) of the child class without any of the parent class. You'll have to change the function a bit if you ever change the class name, but at least for me this was a workable solution.
Parent 是保留名称。
在 Parent1 类中,您可以看到 $a 和 $b 被删除。
将 $c/$c 更改为受保护。
另一个解决方案是:
将 foo 放入 Child
EDIT
抱歉唤醒旧帖子。我认为我有一个更好的解决方案(实际上有两个解决方案):
第一个是使用中间类,它将在父级和子级之间创建屏障:
另一个解决方案是使用可见性:
如果您从类内部调用 get_class_vars()/get_object_vars() ,它会看到所有变量(包括私有/受保护)。如果您从外部运行它,它只会看到公共:
因为这将导致将类字段设置为公共,所以我会选择第一个解决方案。
Parent is a reserved name.
in class Parent1 you can see $a and $b so removed.
changed $c/$c to protected.
the other solution would be:
putting foo in Child
EDIT
Sorry to wake an old post. I think i have a preferable solution (actually 2 solutions) for this:
The first one is to use a middle class that will create a barrier between the parent and the child:
The other solution is to use visibility:
If you call get_class_vars()/get_object_vars() from inside a class, it sees all the variables (including private/protected). If you run it from outside it will only see public:
since this will result in putting class fields as public, I'd go with the first solution.
首先是一些基本错误:
$
(此处:$foo
),这将导致语法错误。Parent
,因为它是保留字。调用此函数会导致类似致命错误:无法使用'Parent'作为类名,因为它是保留的之类的错误,
php手册,可以找到这句重要的话,它回答了你的问题:
First some basic mistakes:
$
in a function name (here:$foo
), this will result into a syntax error.Parent
, because it is a reserved word.Calling this would result into an error like Fatal error: Cannot use 'Parent' as class name as it is reserved
There is a good example how it works in the php manual, and there can be found this important sentence, which answers your question:
将子属性的可见性更改为“受保护”。
当属性是私有的时,它是不可见的。
更多信息请访问:
http://php.net/manual/en/language.oop5 .可见性.php
Change the visibility of Child's properties to PROTECTED.
When properties are private, its not visibles.
More info at:
http://php.net/manual/en/language.oop5.visibility.php