PHP 类子修改父 [英] PHP Class child modifying parent

查看:39
本文介绍了PHP 类子修改父的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个名为 db 的类,它是与我的数据库相关的各种函数的访问器,例如添加行、验证输入或生成 html 表单/表格.

I have a class called db which is an accessor for various functions relating to my database, e.g. adding rows, validating input or generating html forms/tables.

我目前必须有 db_htmldb_valid 等扩展这个 db 类的类.这意味着如果我在一条路线中进行不止一种类型的操作,我就会有多个对象.这也意味着我需要编写针对以下目标的代码:

I currently have to have db_html, db_valid etc. classes which extend this db class. This means if I'm doing more than one type of operation in a single route I have multiple objects. It also means I need to write code which targets:

$db_html=new db_html('table name');
$html=$db_html->make_table();

我在其他 API 中看到的更多是形式:

What I've seen done in other APIs is more the form:

$db=new db('table_name');
$html=$db->html->make_table();

然后也允许:

$db->access->insert([data]);

使用包含数据库架构的单个对象.

With a single object containing the database schema.

但是据我所知,要创建上面的 htmlaccess 组件仍然需要我创建一个扩展类:

However as far as I can see, to create the above html and access components still requires me to create an extended class:

function __construct($name){
  $this->html=new db_html($name);
}

并且对一个孩子的属性的任何后续修改都不会传播到其他孩子,因为即使都是该类的孩子",他们也无法访问其父母"的属性,在这种情况下既是字面的父级又是容器.

And any subsequent modification of the properties of one child will not be propagated to other children, as even though all are "children" of the class, they have no way of accessing properties of their "parent", which in this case is both a literal parent and a container.

我也有可能把自己包裹起来并变得困惑.解决此问题的最佳方法是什么?

There's also a chance I've wrapped myself up and become confused. What would be the best way to resolve this?

推荐答案

在PHP中引用容器对象的方法?

链接问题中已接受的答案已经很好地回答了这个问题.

This question has been well answered by the accepted answer in the linked question.

您必须修改代码以提供关系.在 OOP 中,我们称之为聚合.

You have to modify your code to provide a relationship. in OOP-speak, we call this aggregation.

有关有效示例,请参阅该问题.就我而言,我已解决如下:

See that question for a worked example. In my case I have solved as follows:

abstract class db_master {
protected $children=array();
protected $parent;

function __get($name){
    if (!isset($this->children[$name])){
        $this->add_child($name);
    }
    return $this->children[$name];
}

function link_parent(&$parent){
    $this->parent=$parent;
}

function add_child($name){
    $class='db_'.$name;
    $child=new $class();
    $child->link_parent($this);
    $this->children[$name]=&$child;
}
}

因此,如果我的图书馆中有这个:

Thus if I have this in my library:

class db extends db_master {
    function __construct($name){
    }
}
class db_html extends db_master {
    function generate(){
    }
}

我可以在我的代码中做到这一点:

I can do this in my code:

$db=new db($name);
$html=$db->html->generate();

它可以工作,无需声明子类或进行链接.抽象类可以防止其他人重用代码,以免将 master 误认为是父类.

And it will work, without having to declare the sub class or do the linking. The abstract class prevents someone else reusing the code from mistaking master for the parent class.

这篇关于PHP 类子修改父的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆