如何做一个PHP嵌套类或嵌套方法? [英] How to do a PHP nested class or nested methods?

查看:205
本文介绍了如何做一个PHP嵌套类或嵌套方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在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

我的意思是嵌套方法或嵌套类(我不知道!)
所以当我将限制方法称为用户的孩子时知道我从用户方法 - 或类中调用它 - 当我调用限制方法-or class! - 从注释它也知道。

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() -method的对象,该部分也是有效的。然后, id()需要返回一个具有 limit() -method的对象。

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();

在我的例子中,每个方法(这里也没有描述)将返回对象本身,以便命令可以连在一起当完成数据库查询的构建时,您实际上通过调用 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.

这篇关于如何做一个PHP嵌套类或嵌套方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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