dongtan7998 2018-01-25 04:58
浏览 70
已采纳

从成员对象获取类引用

I am trying to build some classes for a project and I was wondering how to achieve the following. I am not really sure how to ask this with words but I will provide an example:

class Table{
    private $name;
    private $fields = [];

    public function addField(Field $field){
        $this->fields[$field->getName()] = $field;
    }

    public function getName(){
        return $this->name;
    }
}

class Field{
    private $name;

    public function getName(){
        return $this->name;
    }

    public function getTableName(){
        //return Table::getName
    }

    public function getTable(){
        //return a ref to the Table object
    }

}

$table = new Table();
$field = new Field();
$table->addField($field);

What I am trying to achieve here, once the $field is added to the $table, is there some sort of way to get the reference of the $table from any of the methods in the $field object

I would greatly appreciate any help or ideas how to restructure it so I can achieve my goal

Thank you in advance

  • 写回答

1条回答 默认 最新

  • doulun5683 2018-01-25 05:19
    关注
    class Table{
        private $name;
        private $fields = [];
    
        public function addField(Field $field){
            $this->field->setTable($this);
            $this->fields[$field->getName()] = $field;
        }
    
        public function getName(){
            return $this->name;
        }
    }
    
    class Field{
        private $name;
        private $relatedTable;
    
        public function getName(){
            return $this->name;
        }
    
        public function setName($name){
            $this->name = $name;
        }
    
        public function getTableName(){
            return $this->relatedTable->getName();
        }
    
        public function getTable(){
            return $this->relatedTable;
        }
    
        public function setTable(Table $table){
            $this->relatedTable = $table;
        }
    
    }
    
    $field = new Field;
    $field->setName('Field1');
    $table = new Table;
    $table->addField($field);
    echo $field->getTable()->getName();
    

    Although you have to be aware that when you pass an object to a function, it will be passed by "reference" (I know there's another term for this.)

    // in case you're running it in a for loop
    $field = new Field;
    $table = new Table;
    for($i = 0; $i < 3; $i++)
    {
        $field->setName("Field{$i}");
        $table->addField(clone $field); // notice the clone there.
    }
    

    I think this approach is kind of similar with Observer Pattern

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 iqoo11 如何下载安装工程模式
  • ¥15 本题的答案是不是有问题
  • ¥15 关于#r语言#的问题:(svydesign)为什么在一个大的数据集中抽取了一个小数据集
  • ¥15 C++使用Gunplot
  • ¥15 这个电路是如何实现路灯控制器的,原理是什么,怎么求解灯亮起后熄灭的时间如图?
  • ¥15 matlab数字图像处理频率域滤波
  • ¥15 在abaqus做了二维正交切削模型,给刀具添加了超声振动条件后输出切削力为什么比普通切削增大这么多
  • ¥15 ELGamal和paillier计算效率谁快?
  • ¥15 蓝桥杯单片机第十三届第一场,整点继电器吸合,5s后断开出现了问题
  • ¥15 file converter 转换格式失败 报错 Error marking filters as finished,如何解决?