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?
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.
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:
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:
/** @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
. Generic response classes would provide responses, debug messages and a success method.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.