如何通过 php Class 中的私有函数检查变量是否存在?
How to check for a variable existence by a private function in a php Class?
这是我想做的伪代码:
class Test {
protected $myVar;
public function __construct(){
// get data from DB, returns null if not exist
$this->myVar = getDataFromDatabase();
}
public function myFunc(){
if(!$this->myVar){
return response(['message' => 'data not found'],404);
}
// do other stuff
}
}
此代码运行良好。现在我想声明一个私有函数来检查变量是否存在(清理我的代码并在任何地方使用该函数)。
这是我试过的:
public function myFunc(){
$this->checkExistence();
// do other stuff
}
private function checkExistence(){
if(!$this->myVar){
return response(404);
}
}
但是它不起作用,因为我没有return $this->checkExistence();
!
当我尝试 return 时:
return $this->checkExistence();
它正在工作,但问题是当 $myVar
存在时它没有命中 // do other stuff
行,因为它 return 整个函数是有意义的!
这个存在性检查应该如何转为私有函数!?
如果您想 return 在控制器外的某处作出响应,您始终可以使用 abort()
方法。正如 Laravel 文档所说:
The abort function throws an HTTP exception which will be rendered by the exception handler.
该方法接受响应代码,但也接受响应 class(您可以 see here)。如果您想要 return 一个 JSON 响应,您可以使用以下代码:
abort(response()->json([], 404));
您可以抛出(自定义)异常,并在 myFunc() 中处理该异常;
public function myFunc()
{
try {
$this->checkExistence();
// do other stuff
} catch (\My\Namespace\ExistenceException $e) {
//Handle error
return response(404);
}
}
/**
* @throws \My\Namespace\ExistenceException
*/
private function checkExistence(): void
{
if(!$this->myVar){
throw new \My\Namespace\ExistenceException;
}
}
对您来说最好和最简单的方法是在您的私有方法中添加一个 else 子句。 return 语句之后的代码没有 运行,因此在您的情况下 "else" 是强制性的。此外,请尝试将 $myVar 的类型提示也传递给构造函数。
public function __construct($myVar)
{
//assign here as you've done
}
我建议不要在您的 checkExistence()
方法中 returning 响应。相反,我会使用 return true
或 false
和 return myFunc()
:
中的响应
public function myFunc(){
if (this->checkExistence()) {
return response(404);
// Or better yet, use: `abort(404);`
}
// do other stuff
}
private function checkExistence(){
return !$this->myVar;
}
您不想检查变量是否为假(在 PHP 中包括 bool(false) 和 NULL 以及空字符串和不存在的 属性 以及空数组和 god知道还有什么),您想检查 属性 是否存在,对吗?在这种情况下,使用 property_exists
,例如
private function checkExistence(){
if(!property_exists($this,'myVar'){
return response(404);
}
}
这是我想做的伪代码:
class Test {
protected $myVar;
public function __construct(){
// get data from DB, returns null if not exist
$this->myVar = getDataFromDatabase();
}
public function myFunc(){
if(!$this->myVar){
return response(['message' => 'data not found'],404);
}
// do other stuff
}
}
此代码运行良好。现在我想声明一个私有函数来检查变量是否存在(清理我的代码并在任何地方使用该函数)。
这是我试过的:
public function myFunc(){
$this->checkExistence();
// do other stuff
}
private function checkExistence(){
if(!$this->myVar){
return response(404);
}
}
但是它不起作用,因为我没有return $this->checkExistence();
!
当我尝试 return 时:
return $this->checkExistence();
它正在工作,但问题是当 $myVar
存在时它没有命中 // do other stuff
行,因为它 return 整个函数是有意义的!
这个存在性检查应该如何转为私有函数!?
如果您想 return 在控制器外的某处作出响应,您始终可以使用 abort()
方法。正如 Laravel 文档所说:
The abort function throws an HTTP exception which will be rendered by the exception handler.
该方法接受响应代码,但也接受响应 class(您可以 see here)。如果您想要 return 一个 JSON 响应,您可以使用以下代码:
abort(response()->json([], 404));
您可以抛出(自定义)异常,并在 myFunc() 中处理该异常;
public function myFunc()
{
try {
$this->checkExistence();
// do other stuff
} catch (\My\Namespace\ExistenceException $e) {
//Handle error
return response(404);
}
}
/**
* @throws \My\Namespace\ExistenceException
*/
private function checkExistence(): void
{
if(!$this->myVar){
throw new \My\Namespace\ExistenceException;
}
}
对您来说最好和最简单的方法是在您的私有方法中添加一个 else 子句。 return 语句之后的代码没有 运行,因此在您的情况下 "else" 是强制性的。此外,请尝试将 $myVar 的类型提示也传递给构造函数。
public function __construct($myVar)
{
//assign here as you've done
}
我建议不要在您的 checkExistence()
方法中 returning 响应。相反,我会使用 return true
或 false
和 return myFunc()
:
public function myFunc(){
if (this->checkExistence()) {
return response(404);
// Or better yet, use: `abort(404);`
}
// do other stuff
}
private function checkExistence(){
return !$this->myVar;
}
您不想检查变量是否为假(在 PHP 中包括 bool(false) 和 NULL 以及空字符串和不存在的 属性 以及空数组和 god知道还有什么),您想检查 属性 是否存在,对吗?在这种情况下,使用 property_exists
,例如
private function checkExistence(){
if(!property_exists($this,'myVar'){
return response(404);
}
}