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

发布于 2024-07-13 15:02:09 字数 541 浏览 12 评论 0原文

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

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

我的意思是嵌套方法或嵌套类(我不知道!) 因此,当我作为用户的子级调用 limit 方法时,它会知道我是从“用户”方法或类中调用它,并且当我从注释中调用 limit 方法或类时,它也知道这一点。

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


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

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

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

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

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.

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

和我恋爱吧 2024-07-20 15:02:09

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

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

在您的特定情况下,您可能想做这样的事情:

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() 来评估查询,(在我的例子中)将返回一个布尔值,表示数据库执行是否成功。

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

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

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

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

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.

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

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.

我们的影子 2024-07-20 15:02:09

标准约定是在每个方法调用结束时返回 $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();

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.

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

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

$foo = new Foo();
$foo->do_something()->do_something_else();
心凉 2024-07-20 15:02:09

一个简单的实施方法可以帮助您开始:

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

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

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

交替,但更强大:

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

你明白了。 重载数据库对象,或者创建基本数据库对象和表对象,将大部分智能放入表对象中,确保创建时表对象存储对数据库对象的引用

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文