Jump to content

Can I return an instance of a class when accessing class constants


Recommended Posts

I have the following class:

SGNBEResponseCode class:

<?php
namespace SGNBackend\SGNBEResponseCodes;

class SGNBEResponseCode {
	public function __construct(){
		print_r(__CLASS__);
		return __CLASS__;
	}
}
?>
<?php
namespace SGNBackend\SGNBEResponseCodes;

class Common extends SGNBEResponseCode {
	public const UNKNOWN_ERROR = -1;
	public const INVALID_REQUEST = 0;
	public const REQ_FAILED = 100;
	public const DB_QUERY_ERROR = 101;
	public const DB_COUNT_ERROR = 102;
	public const DB_FETCH_ERROR = 103;
	public const EMPTY_PARAM_VALUE = 104;
	public const INVALID_PARAM_VALUE = 105;
	public const REQ_DONE = 106;

	public function __construct(){
		print_r(__CLASS__);
		return __CLASS__;
	}
}
?>
Here SGNBEResponse class:

<?php
namespace SGNBackend;

class SGNBEResponse{
	const response = array("error"=>true, "code"=>-1, "name"=>"UNKNOWN_ERROR", "message"=>"An unknown error has occurred");

	public static function generateErrorFromCode(SGNBEResponseCode $code, string $args = null){
		$c = debug_backtrace();
		print_r($c);
		//return "test";
	}
}
?>
And I'm trying to access the constants like this:

<?php
$sgnbe = new \SGNBackend\SGNBEResponse();
print_r(\SGNBackend\SGNBEResponse::generateErrorFromCode(\SGNBackend\SGNBEResponseCodes\Common::INVALID_REQUEST));
?>
And when I access the constants an instance of the class along with the name of the constant and value should be returned instead of the value of the constants. Can I do it? If yes then how?
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...