Jump to content


Photo

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


  • Please log in to reply
No replies to this topic

#1 sagnik

sagnik

    Rank VI Member

  • Members
  • PipPipPipPipPipPip
  • 437 posts
  • Gender:Male
  • Location:Durgapur, West Bengal, India
  • Interests:Internet, Computer, Coding, Website Designing

Posted 08 January 2020 - 01:07 PM

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?
- By Sagnik




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users