PHP5和类继承问题 [英] PHP5 and class inheritance question
问题描述
我有什么可能是一个bug,或者可能是误解了如何通过
引用和类继承在PHP中工作。因为我是PHP的新手,所以我希望有一点外面的帮助来解释这个问题吧
这个!
>
(代码缩写为清晰)
我有一个父类,DetailCollection,带有子类
KeycodeTracker:
class DetailCollection {
private $ aODetails;
private function setDetail(& $ oDetail,$ bEcho = false){
$ this-> aODetails [] = $ oDetail;
if($ bEcho){echo(count(" DetCol"。$ this-> aODetails。"< BR> ;")); }
}
}
类KeycodeTracker扩展DetailCollection {
私函数setDetail(& $ oDetail){
DetailCollection :: setDetail($ oDetail,true);
echo(count(" KeyTrk"。$ this-> aODetails。"< ; BR>"));
}
}
DetailCollection有一个名为setDetail的方法,它接受一个对象
通过引用传递并将其存储在数组中。这在
另一个覆盖DetailCollection的setDetail的子类中工作正常,但是
KeycodeTracker使用对原始类的调用而不起作用 -
$ this-> aODetails中没有元素。
这与通过引用覆盖传递有什么关系
方法办法?或者我只是我自己新手的受害者?
谢谢你偷看!
泰勒
I have what may be a bug, or may be a misunderstanding on how pass by
reference and class inheritance works in PHP. Since I''m relatively
new to PHP, I''m hoping for a little outside help to shed some light on
this!
(code abbreviated for clarity)
I have a parent class, DetailCollection, with a child class
KeycodeTracker:
class DetailCollection{
private $aODetails;
private function setDetail(&$oDetail, $bEcho=false) {
$this->aODetails[] =$oDetail;
if($bEcho) { echo(count("DetCol".$this->aODetails."<BR>")); }
}
}
class KeycodeTracker extends DetailCollection{
private function setDetail(&$oDetail) {
DetailCollection::setDetail($oDetail, true);
echo(count("KeyTrk".$this->aODetails."<BR>"));
}
}
DetailCollection has a method called setDetail, which takes an object
passed by reference and stashes it in an array. This works fine in
another child class that overrides DetailCollection''s setDetail, but
KeycodeTracker uses a call to the original class and does not work -
there are no elements in $this->aODetails.
Does this have anything to do with overriding a pass by reference
method in some way? Or am I just a victim of my own newbieness here?
Thanks for taking a peek!
Tyler
推荐答案
aODetails;
私人函数setDetail(&
aODetails;
private function setDetail(&
oDetail,
bEcho = false){
bEcho=false) {
这篇关于PHP5和类继承问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!