AFAIK,您不能在类范围内的方法之外有条件(如果流动)
Class Test {
if (empty($Var)){
public function Test_Method (){
}
}
}
不管用。为什么不让它一直存在,而只在需要时调用该方法?
例子:
Class Test {
public function Some_Method(){
return 23094; // Return something for example purpose
}
}
然后从您的 PHP 中:
$Var = ""; // set an empty string
$Class = new Test();
if (empty($Var)){
echo $Class->Some_Method(); // Will output if $Var is empty
}
也许您试图验证 OOP 范围内的字符串,然后举这个例子:
Class New_Test {
public $Variable; // Set a public variable
public function Set(){
$This->Variable = "This is not empty"; // When calling, $this->variable will not be empty
}
public function Fail_Safe(){
return "something"; // return a string
}
}
然后超出范围:
$Class = new New_Test();
if (empty($Class->Variable)){
$Class->Fail_Safe();
} // Call failsafe if the variable in OOP scope is empty