久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

如何做一個 PHP 嵌套類或嵌套方法?

How to do a PHP nested class or nested methods?(如何做一個 PHP 嵌套類或嵌套方法?)
本文介紹了如何做一個 PHP 嵌套類或嵌套方法?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

我如何在 PHP 中執(zhí)行此操作

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

我的意思是嵌套方法或嵌套類(我不知道!)所以當我作為用戶的孩子調(diào)用 limit 方法時,它會知道我是從用戶"方法 - 或類 - 調(diào)用它時,當我從評論中調(diào)用 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 類執(zhí)行此操作的可能結構是什么?

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

這個問題的原因是因為我正在為自己的數(shù)據(jù)庫類工作,所以我可以輕松地使用這樣的東西

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.

在您的特定情況下,您可能想要執(zhí)行以下操作:

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

在我的示例中,每個方法(此處也未描述)將返回對象本身,以便可以將命令鏈接在一起.當數(shù)據(jù)庫查詢的構建完成后,您實際上通過調(diào)用 execute() 來評估查詢,它(在我的例子中)將返回一個代表數(shù)據(jù)庫執(zhí)行成功的布爾值.

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 嵌套類或嵌套方法?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權益,請聯(lián)系我們刪除處理,感謝您的支持!

相關文檔推薦

Cannot use #39;Object as class name as it is reserved Cake 2.2.x(不能使用 Object 作為類名,因為它是保留的 Cake 2.2.x)
Session is lost after an OAuth redirect(OAuth 重定向后會話丟失)
Pagination Sort in Cakephp 3.x(Cakephp 3.x 中的分頁排序)
CakePHP Shared core for multiple apps(CakePHP 多個應用程序的共享核心)
Login [ Auth-gt;identify() ] always false on CakePHP 3(在 CakePHP 3 上登錄 [ Auth-identify() ] 始終為 false)
Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 87 bytes)(致命錯誤:允許的內(nèi)存大小為 134217728 字節(jié)已用盡(嘗試分配 87 字節(jié)))
主站蜘蛛池模板: 久久大香 | 黄色精品 | 精品国产乱码久久久久久图片 | 久久精品视频网站 | 成人精品在线视频 | 日韩一区二区三区av | 2022精品国偷自产免费观看 | 中文字幕视频在线 | 亚洲一区二区三区在线播放 | av在线免费观看网站 | 国产91中文| 色接久久 | 久久99精品国产 | 亚洲激情一级片 | 人干人操| 亚洲欧洲一区 | 日韩a在线| 国产精品久久久久久久模特 | 国产精品一级 | 中国一级特黄视频 | 久久精品国产亚洲a | 国产精品久久久久久久久久 | 亚洲精品一 | 欧美一区2区三区3区公司 | 99这里只有精品视频 | 久久人爽爽人爽爽 | 狠狠躁18三区二区一区 | 91麻豆精品国产91久久久更新资源速度超快 | 毛片入口 | 亚洲欧美自拍偷拍视频 | 91精品国产麻豆 | 丁香一区二区 | 欧美性另类 | 中文字幕 在线观看 | 亚洲国产一区在线 | 国产乱码精品一区二区三区中文 | www.se91| 国产综合网址 | 欧美日韩精品专区 | 亚洲欧美日韩精品久久亚洲区 | 国产精品1区|