PHP-在对象中调用对象
我有一个用MYSQL和PHP创建OODB的类项目 目前,我的表中填充了对象框。我还有一个box类,当它被构造时,它将从表中提取数据,然后以类似的方式递归地构造它的子类。这似乎效果不错。但我无法从子框调用函数 下面是课堂:PHP-在对象中调用对象,php,mysql,oodb,Php,Mysql,Oodb,我有一个用MYSQL和PHP创建OODB的类项目 目前,我的表中填充了对象框。我还有一个box类,当它被构造时,它将从表中提取数据,然后以类似的方式递归地构造它的子类。这似乎效果不错。但我无法从子框调用函数 下面是课堂: class Box1 { var $id; var $pid; var $children; var $type; var $content; var $siblingorder; function Box1($bId)
class Box1 {
var $id;
var $pid;
var $children;
var $type;
var $content;
var $siblingorder;
function Box1($bId){
$q ="SELECT * FROM `box` WHERE id =". $bId;
$r = mysql_query($q);
if ($r){
$row = mysql_fetch_array($r);
$this->id=$bId;
$this->pid=$row[1];
$this->children=$row[2];
$this->type=$row[3];
$this->siblingorder=$row[5];
$this->content=$row[6];
//echo $this->id."<br />";
if(isset($this->children)){
//echo $this->children."<br />";
$kids = explode(',', $this->children);
foreach ($kids as $key => $value) {
$varname = "box".$value;
//global $$varname;
//echo $varname."<br>";
$$varname = new Box1($value);
}
}
}
}//constructor
function display(){
echo "<div style='border: solid 2px;'>";
echo $this->id;
echo "<br />";
if(isset($this->children)){
$kids = explode(',', $this->children);
foreach ($kids as $key => $value) {
$varname = "box".$value;
//echo $varname."<br />";
$$varname->display();
}
}
echo "</div>";
}//End DISPLAY
function update(){
}//End UPDATE
}
任何帮助或见解都将不胜感激。如第一条评论中所述,问题在于$$varname是在构造函数内部创建和分配的。但它不存在于功能显示中。一旦承包商 已调用,这些变量不再存在。在下面查找一些代码,这些代码向您展示了如何使子对象成为Box1类型的对象数组
class Box1 {
var $id;
var $pid;
var $children;
var $type;
var $content;
var $siblingorder;
function Box1($bId){
$q ="SELECT * FROM `box` WHERE id =". $bId;
$r = mysql_query($q);
if ($r){
$row = mysql_fetch_array($r);
$this->id=$bId;
$this->pid=$row[1];
$this->children = array();//[*]
$this->type=$row[3];
$this->siblingorder=$row[5];
$this->content=$row[6];
//now we fill this->children with objects of type Box1
if ($row[2] != '') {
$kids = explode(',', $row[2]);
foreach ($kids as $value) {
$this->children[] = new Box1($value);
}
}
}
}//constructor
function display(){
echo "<div style='border: solid 2px;'>";
echo $this->id;
echo "<br />";
foreach ($this->chidren as $kid) {
$kid->display();
}
echo "</div>";
}//End DISPLAY
function update(){
}//End UPDATE
}
class-Box1{
var$id;
var$pid;
var$儿童;
var$类型;
var$内容;
var$siblingorder;
功能盒1($bId){
$q=“从'box'中选择*,其中id=”.$bId;
$r=mysql\u查询($q);
如果($r){
$row=mysql\u fetch\u数组($r);
$this->id=$bId;
$this->pid=$row[1];
$this->children=array();//[*]
$this->type=$row[3];
$this->siblingorder=$row[5];
$this->content=$row[6];
//现在我们用Box1类型的对象填充->子对象
如果($row[2]!=''){
$kids=explode(“,”,$row[2]);
foreach(儿童为$value){
$this->children[]=新框1($value);
}
}
}
}//建造师
函数显示(){
回声“;
echo$this->id;
回声“
”;
foreach($this->children as$kid){
$kid->display();
}
回声“;
}//终端显示
函数更新(){
}//结束更新
}
[*]:这里我们决定children始终是Box1的数组。当然,如果没有子数组,则该数组可以为空。这是口味的问题,如果没有孩子,有些人宁愿让它无效。但在这种情况下,在display()中迭代$this->children之前,必须检查null值。这是变量范围的问题
display()
无法访问$box1
,因为它不在该函数的范围内。我可能会创建一个children对象数组作为对象的属性。经过一些非常小的调整,比如添加l到->children,它就工作了。非常感谢你。
class Box1 {
var $id;
var $pid;
var $children;
var $type;
var $content;
var $siblingorder;
function Box1($bId){
$q ="SELECT * FROM `box` WHERE id =". $bId;
$r = mysql_query($q);
if ($r){
$row = mysql_fetch_array($r);
$this->id=$bId;
$this->pid=$row[1];
$this->children = array();//[*]
$this->type=$row[3];
$this->siblingorder=$row[5];
$this->content=$row[6];
//now we fill this->children with objects of type Box1
if ($row[2] != '') {
$kids = explode(',', $row[2]);
foreach ($kids as $value) {
$this->children[] = new Box1($value);
}
}
}
}//constructor
function display(){
echo "<div style='border: solid 2px;'>";
echo $this->id;
echo "<br />";
foreach ($this->chidren as $kid) {
$kid->display();
}
echo "</div>";
}//End DISPLAY
function update(){
}//End UPDATE
}