如何做一个 PHP 嵌套类或嵌套方法?

2021-12-22 00:00:00 design-patterns php fluent-interface

我如何在 PHP 中执行此操作

How can I do this in PHP

$myDBClass->users()->limit(5);//output you limited users to 5
$myDBClass->comments()->limit(3);//output you limited comments to 3

我的意思是嵌套方法或嵌套类(我不知道!)所以当我作为用户的孩子调用 limit 方法时,它会知道我是从用户"方法 - 或类 - 调用它时,当我从评论中调用 limit 方法 - 或类! - 它也知道这一点.

what I meant is nested methods or nested class (I don't know!) so when I call the limit method as a child of users it will know that I am calling it from "users" method -or class- and when I call limit method -or class!- from comments It also knows that.

PHP 类执行此操作的可能结构是什么?

what is the possible structure for a PHP class to do this thing?

这个问题的原因是因为我正在为自己的数据库类工作,所以我可以轻松地使用这样的东西

the reason for this question because I am working on my own class for database so I can easily use something like this

     $DB->comments()->id(" > 3")->limit(10);

生成sql代码select * from comments where id > 3 limit 10"谢谢

to generate the sql code "select * from comments where id > 3 limit 10" Thanks

推荐答案

让方法返回带有所描述方法的对象,你就会得到你想要的.

Have the methods return objects with the methods described, and you get what you are after.

因此,只要 $DB 是具有 comments() 方法的对象,该部分就是有效的.如果 comments() 返回一个具有 id() 方法的对象,那么该部分也是有效的.然后,id() 需要返回一个具有 limit() 方法的对象.

So, as long as $DB is an object that has a comments()-method, that part is valid. If that comments() returns an object that has an id()-method, that part is valid, too. Then, id() needs to return an object that has the limit()-method.

在您的特定情况下,您可能想要执行以下操作:

In your particular case, you might want to do something like this:

class DB {
  public function comments() {
    // do preparations that make the object select the "comments"-table...
    return $this;
  }

  public function id($string) {
    // handle this too...
    return $this;
  }

  public function limit($int) {
    // also this
    return $this;
  }

  public function execute() {
    $success = try_to_execute_accumulated_db_commands();
    return $success;
  }
}

$DB = new DB();
$DB->comments()->id(" > 3")->limit(10);

在我的示例中,每个方法(此处也未描述)将返回对象本身,以便可以将命令链接在一起.当数据库查询的构建完成后,您实际上通过调用 execute() 来评估查询,它(在我的例子中)将返回一个代表数据库执行成功的布尔值.

In my example, every method (also not depicted here) would return the object itself, so that commands can be chained together. When the construction of the database query is done, you actually evaluate the query by invoking execute() that (in my case) would return a boolean that would represent the success of the database execution.

用户 nickohm 建议将其称为流畅界面.我必须承认,这对我来说是一个新术语,但这可能比该术语的用法更能说明我的知识.(我只是写代码,你知道……")

User nickohm suggested that this is called a fluent interface. I must admit that this is a new term for me, but that tells probably more of my knowledge, than the term's usage. ("I just write code, you know...")

注意: $this 是一个魔法"变量,指向当前活动的对象.顾名思义,它只是返回自身作为方法的返回值.

Note: $this is a 'magic' variable that points to the currently active object. As the name suggests, it just returns itself as the return value for the method.

相关文章