[英]Return Objects (PHP Best Practices)
When writing PHP OOP code, is it a good / acceptable / wise practice to use "return objects" for your various classes in order to pass along success, failure, error messages, etc. up the food chain? 在编写PHP OOP代码时,在各个类中使用“返回对象”以在食物链中传递成功,失败,错误消息等,是一种好的/可接受的/明智的做法?
Example of what I have now: 我现在所拥有的示例:
"Return object": “返回对象”:
class JsqlReturn{
public $response;
public $success;
public $debug_message;
public $mysqli_result_obj;
function __construct($bool=false,$debug_message=NULL,$res=NULL,$mysqli_result_obj=NULL){
$this->success = $bool;
$this->response = $res;
$this->debug_message = $debug_message;
$this->mysqli_result_obj = $mysqli_result_obj;
}
}
Main Class with sample method: 具有示例方法的主类:
class Jsql{
function connect($host,$username,$password,$database){ #protected?
$this->db = new \mysqli($host,$username,$password,$database);
if($this->db->connect_errno){
return new JsqlReturn(false,"Connection failed: (".$this->db->connect_errno.") ".$this->db->connect_error);
}
else{
return new JsqlReturn(true,NULL,"Connection success.");
}
}
}
Implementation: 执行:
$db = new Jsql;
$return = $db->connect(...);
if($return->success){ echo $return->response; }
else{ echo $return->debug_message; }
I know using a connect example here is trivial, but my question relates to the coding practice. 我知道在这里使用连接示例是微不足道的,但是我的问题与编码实践有关。
My main goal in this practice is to ensure I'm being consistent in how I am handling the return data from methods. 我在此实践中的主要目标是确保我在处理方法的返回数据方面保持一致。
Notes: have mercy. 备注:请留意。 This is my first question on here. 这是我的第一个问题。 :) I've been slowly self taught taking the normal route from dabbling in html years ago to moving toward procedural php and finally getting into OOP. :)我一直在慢慢地自学成才,从几年前涉猎html到使用程序php并最终进入OOP。
It seems like a perfectly reasonable approach to me. 对我来说,这似乎是一种完全合理的方法。
In terms of being consistent in how I am handling the return data from methods
you could make the response class implement a Response interface , then you'll know that all types of response classes will adhere to the same rules so you can safely use it throughout your application: 就being consistent in how I am handling the return data from methods
而言,可以使响应类实现Response 接口 ,然后您将知道所有类型的响应类都将遵循相同的规则,因此可以在整个过程中安全地使用它你的申请:
interface MyResponseInterface
{
public function getResponse();
public function getDebugMessage();
public function getSuccess();
public function getMysqliConnection();
}
class JsqlResponse implements MyResponseInterface
{
// ...
}
Then you know that whenever your object returns either a JsqlResponse
, a TimeResponse
, a MemberResponse
etc, that they shall all implement your response interface, and as such your public getters will be available, eg: 然后,您知道只要您的对象返回JsqlResponse
, TimeResponse
, MemberResponse
等,它们都将实现您的响应接口,因此您的公共获取器将可用,例如:
/** @var MyResponseInterface $return */
$return = $db->connect(...);
if($return->getSuccess()) {
echo $return->getResponse();
} else {
echo $return->getDebugMessage();
}
Note: In my example of different kinds of responses you could return, I'd imagine (hypothetically) that Time and Member may not need a MySQL connection, so perhaps you could omit that from the MyResponseInterface
and create a new interface for database connections, eg MyDatabaseInterface
. 注意:在我可能返回的各种响应的示例中,我想(假设),Time和Member可能不需要MySQL连接,因此也许您可以从MyResponseInterface
忽略它,并为数据库连接创建一个新接口,例如MyDatabaseInterface
。 Generic response classes would provide responses, debug messages and a success method. 通用响应类将提供响应,调试消息和成功方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.