Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
检查setter是否设置为PHP_Php_Oop_Getter Setter - Fatal编程技术网

检查setter是否设置为PHP

检查setter是否设置为PHP,php,oop,getter-setter,Php,Oop,Getter Setter,嗨,有没有办法检查类中的setter是否已设置 我尝试了is_object和isset,但没有得到正确的结果 例如: class FruitsModel{ public $fruitId; public function setFruitId($fruitId){ $this->fruitId = $fruitId; } public function displayFruit() { if(SETTER_FRU

嗨,有没有办法检查类中的setter是否已设置

我尝试了is_object和isset,但没有得到正确的结果

例如:

class FruitsModel{

    public $fruitId;

    public function setFruitId($fruitId){
        $this->fruitId = $fruitId;
    }

    public function displayFruit()
    {
        if(SETTER_FRUIT IS NOT SET){throw new Exception("FruitId is missing!");}

        echo $this->fruitId;

    }

}

您应该做的是将$FROUTID初始化为null作为默认值,然后您可以说:
if($this->FROUTID==null){}


顺便说一句,将属性设置为私有,您不想让setter带有公共变量:)

您应该做的是将$FROUTID初始化为null作为默认值,然后您可以说:
如果($this->FROUTID==null){}


顺便说一句,将属性设置为私有,您不希望设置带有公共变量的setter:)

您可以尝试使用
method\u exits()
。检查类中是否存在方法

比如:

$directory = new Directory('.');
var_dump(method_exists($directory,'read')); //method_exists return a bool

有关更多文档,请查看此链接:

您可以尝试使用
方法\u exits()
。检查类中是否存在方法

比如:

$directory = new Directory('.');
var_dump(method_exists($directory,'read')); //method_exists return a bool

有关更多文档,请查看此链接:

您可以在类中创建函数

class FruitsModel {
    public function variableExists($key) {
        $retVal = false;
        if(isset($this->$key) == true) {
            $retVal = true;
        }

        return $retVal;
    }
}
用法:

$fm = new FruidsModel();
echo $fm->variableExists($fruitId); //true

你可以在你的类中创建一个函数

class FruitsModel {
    public function variableExists($key) {
        $retVal = false;
        if(isset($this->$key) == true) {
            $retVal = true;
        }

        return $retVal;
    }
}
用法:

$fm = new FruidsModel();
echo $fm->variableExists($fruitId); //true

为变量的值创建新方法,如果希望其他类无权访问该方法,则该方法可以是私有的:

class FruitsModel{

    public $fruitId = "";

    public function setFruitId($fruitId){
        $this->fruitId = $fruitId;
    }


   private function getFruitId(){

     return $this->$fruitId;

  }


    public function displayFruit()
    {
        if($this->getFruitId()  == ""){throw new Exception("FruitId is missing!");}

        echo $this->fruitId;

    }

}

为变量的值创建新方法,如果希望其他类无权访问该方法,则该方法可以是私有的:

class FruitsModel{

    public $fruitId = "";

    public function setFruitId($fruitId){
        $this->fruitId = $fruitId;
    }


   private function getFruitId(){

     return $this->$fruitId;

  }


    public function displayFruit()
    {
        if($this->getFruitId()  == ""){throw new Exception("FruitId is missing!");}

        echo $this->fruitId;

    }

}

开发人员应该知道类中需要实现哪些方法。假设他没有,我们怎么能强迫他实现它们,而不通过编程检查其他方法中某些方法的存在

这就是界面派上用场的地方以及它们的用途。将接口视为定义类必须实现哪些方法的契约

因此,处理任务最干净的方法就是实现一个接口

FruitsModelInterface.php

<?php

interface FruitsModelInterface{

    public function setFruitId($fruitId);

}
<?php

class FruitsModel implements FruitsModelInterface{

    protected $fruitId;

    public function setFruitId($fruitId){
        $this->fruitId = $fruitId;
    }

    public function displayFruit()
    {
        if(is_null($this->fruitId))
            throw new FruitsModelException('Fruit ID missing!');
        echo $this->fruitId;
        // You'd probably better go with calling the Method
        // getFruit() though and return $this->fruitId instead of echoing
        // it. It's not the job ob the FruitsModel to output something
    }

}

开发人员应该知道类中需要实现哪些方法。假设他没有,我们怎么能强迫他实现它们,而不通过编程检查其他方法中某些方法的存在

这就是界面派上用场的地方以及它们的用途。将接口视为定义类必须实现哪些方法的契约

因此,处理任务最干净的方法就是实现一个接口

FruitsModelInterface.php

<?php

interface FruitsModelInterface{

    public function setFruitId($fruitId);

}
<?php

class FruitsModel implements FruitsModelInterface{

    protected $fruitId;

    public function setFruitId($fruitId){
        $this->fruitId = $fruitId;
    }

    public function displayFruit()
    {
        if(is_null($this->fruitId))
            throw new FruitsModelException('Fruit ID missing!');
        echo $this->fruitId;
        // You'd probably better go with calling the Method
        // getFruit() though and return $this->fruitId instead of echoing
        // it. It's not the job ob the FruitsModel to output something
    }

}

?但是您是在检查方法还是属性?在意外情况下抛出异常的类:Good。一种类方法,它使用
echo
,而不是
return
=>baaad。抛出并使用setter的类不应该回显?但是你是在检查方法,还是属性?在意外情况下抛出异常的类:Good。一种类方法,它使用
echo
,而不是
return
=>baaad。抛出并使用setter的类不应该是echomethod函数不适用于的类variables@mazzy:OP的措辞令人困惑,可能被解释为询问“如何检查是否调用了方法”,但您可以检查setter方法是否存在,否则,如果您正在检查属性是否有值,则可以使用简单的
if
设置setter的意思是如果变量已被赋值,而不是如果函数已存在,则方法函数不适用于variables@mazzy:OP的措辞令人困惑,可能被解释为询问“如何检查是否调用了一个方法”但是你可以检查setter方法是否存在,否则如果你正在检查属性是否有值,你可以使用一个简单的
if
如果setter被设置,那么他的意思是变量是否被赋值,而不是函数是否存在