防止循环引用 [英] Prevent a circular reference
本文介绍了防止循环引用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要你的帮助。如果我将鼓添加到已经属于鼓的cy钹中,那么我创建一个圆形参考。调用getDescription()时,这样的行可能会导致Web服务器崩溃。我想防止这种错误。我的策略是在add()方法期间检查以查看正在添加的工具是否已包含对调用该方法的工具的引用。我的伪代码是:
Hi,
I need your help. If I add the drums to the cymbals that already belong to drums then I create a circular reference. When calling getDescription() a line like this can crash a Web Server. I want to safeguard against such mistakes. My strategy would be to check during the add() method to see whether the instrument being added already contains a reference to the one calling the method.My pseudo-code is:
- 检查鼓是否有孩子;
- 如果有孩子,则将鼓与cy钹比较:
- 如果等于则返回true,否则返回false ;
- 我的逻辑/伪代码是否正确?
- 要将此伪代码转换为PHP,创建递归方法或交互方法是否是个好主意?
- 还有另一种方法检查循环引用?如果是这样,请告诉我!
<html>
<body>
<head>
<style>
body{ font:12px Verdana, Geneva, sans-serif; font-weight:bold}
td{ font:11px Verdana, Geneva, sans-serif;}
</style>
</head>
<?php
abstract class AbstractInstrument{
private $name;
private $category;
private $instruments = array();
public function add(AbstractInstrument $instrument){
array_push($this->instruments, $instrument);
}
public function remove(AbstractInstrument $instrument){
array_pop($this->instruments, $instrument);
}
public function hasChildren(){
return (bool) (count($this->instruments) > 0);
}
public function getChild($i){
return $instruments[$i];
}
public function getDescription(){
echo "- one ".$this->getName();
if($this->hasChildren()){
echo " which includes:<br>";
foreach($this->instruments as $instrument){
echo "<table cellspacing=\"5\" border=\"0\"><tr><td> </td><td>-";
$instrument->getDescription();
echo "</td></tr></table>";
}
}
}
public function setName($name){
$this->name=$name;
}
public function getName(){
return $this->name;
}
public function setCategory($category){
$this->category=$category;
}
public function getCategory(){
return $this->category;
}
}
class Guitar extends AbstractInstrument{
function __construct($name){
parent::setName($name);
parent::setCategory("guitars");
}
}
class DrumSet extends AbstractInstrument{
function __construct($name){
parent::setName($name);
parent::setCategory("drums");
}
}
class SnareDrum extends AbstractInstrument{
function __construct($name){
parent::setName($name);
parent::setCategory("snare drums");
}
}
class BaseDrum extends AbstractInstrument{
function __construct($name){
parent::setName($name);
parent::setCategory("base drums");
}
}
class Cymbal extends AbstractInstrument{
function __construct($name){
parent::setName($name);
parent::setCategory("cymbals");
}
}
$drums = new DrumSet("tama maple set");
$drums->add(new SnareDrum("snare drum"));
$drums->add(new BaseDrum("large bass drum"));
$cymbals = new Cymbal("zildjian cymbal set");
$cymbals->add(new Cymbal("small crash"));
$cymbals->add(new Cymbal("large high hat"));
$drums->add($cymbals);
//$cymbals->add($drums); //Uncomment this and then it will show an error!!
$guitar = new Guitar("gibson les paul");
echo "List of instruments: <p>";
$drums->getDescription();
$guitar->getDescription();
?>
</body>
</html>
推荐答案
名称跨度>;
private
name; private
category ;
private
category; private
instruments = array();
public function add(AbstractInstrument
instruments = array(); public function add(AbstractInstrument
这篇关于防止循环引用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文