問題描述
我如何在 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)!