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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/533459/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 23:00:29  来源:igfitidea点击:

How to do a PHP nested class or nested methods?

phpdesign-patternsfluent-interface

提问by ahmed

How can I do this in PHP

我怎样才能在 PHP 中做到这一点

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

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.

我的意思是嵌套方法或嵌套类(我不知道!)所以当我作为用户的孩子调用 limit 方法时,它会知道我是从“用户”方法或类调用它,当我调用限制方法 - 或类! - 从评论它也知道。

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

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



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

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

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

回答by Henrik Paul

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

让这些方法返回带有所描述方法的对象,然后你就得到了你想要的东西。

So, as long as $DBis 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.

因此,只要$DB是具有comments()-method的对象,该部分就是有效的。如果它comments()返回一个具有id()-method的对象,则该部分也是有效的。然后,id()需要返回一个具有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);

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.

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

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...")

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

Note:$thisis 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.

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

回答by Antonio Haley

The standard convention for this is to return the instance of $this at the end of each of the method call. So when returned to the caller we are then just referencing another method call.

对此的标准约定是在每个方法调用结束时返回 $this 的实例。所以当返回给调用者时,我们只是在引用另一个方法调用。

class Foo
{
  public function do_something()
  { 
    return $this; 
  }

 public function do_something_else() 
 {
   return $this; 
  }
}

$foo = new Foo();
$foo->do_something()->do_something_else();

回答by Mainegreen

A simple to implement method to get you started might go like:

一个简单的实现方法可以让您入门:

class db
{
  public function __call($function, $arguments)
  {
    switch($function) {
      // implement table handling here
      case 'user':
        //do something
        return $something;
        break;
    }
  }
}

Depending on whether or not you want to go complicated, but solid or simple, but less flexible you might implement two different strategies. Simple strategy might go like so:

根据您是否想要变得复杂,但扎实或简单,但不太灵活,您可能会实施两种不同的策略。简单的策略可能是这样的:

class db
{

  protected $operatingTable;

  public function limit($limitNumber)
  {
    return $this->executeQuery("SELECT * FROM ".$this->operatingTable." LIMIT ".$limitNumber); // where executeQuery is a function that runs a query
  }

  public function __call($function, $arguments)
  {
    switch($function) {
      // implement table handling here
      case 'user':
        $this->operatingTable='user'; // alternately, but less secure: $this->operatingTable=$function;
        return $this;
        break;
    }
  }
}

Alternately, but more powerful:

或者,但更强大:

class db
{
  protected $operatingTable;

  public function limit($limitNumber)
  {
    return $this->executeQuery("SELECT * FROM ".$this->operatingTable." LIMIT ".$limitNumber); // where executeQuery is a function that runs a query
  }

  public function __call($function, $arguments)
  {
    switch($function) {
      // implement table handling here
      case 'user':
        $user = new user($this); // pass in the database to the object, so the table object can have a reference to the db
        return $user;
        break;
    }
  }
}

class baseTableClass
{
  protected $db; // an instance of class db

  function limit($limitNumber)
  {
    $db->execute($aStatementDerivedFromThisClassesInformation); // execute a sql command, based on information about the table in the class
  }

}

class user extends baseTableClass
{
  public function __construct($db) {
    $this->db = $db;
  }
}

You get the idea. Either overload the db object, or create a base db object, and table objects, putting much of the intelligence in table objects, ensuring that when created, a table object stores a reference to the db object

你明白了。要么重载 db 对象,要么创建一个基础 db 对象和 table 对象,将大部分智能放在 table 对象中,确保在创建时,table 对象存储对 db 对象的引用