Question

Hi is there a way to check if the setter in a class is set?

I've tried is_object and isset but without a proper result.

Example:

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;

    }

}
Was it helpful?

Solution

A developer should know which methods need to be implemented in the class. Assuming that he does not, how could we force him to implement them without checking the existance of certain methods in your other methods programatically?

That's where interfaces come in handy and what they are for. See an interface as a contract that defines which methods a class must implement.

So the cleanest way to tackle your task would be to just implement an interface.

FruitsModelInterface.php

<?php

interface FruitsModelInterface{

    public function setFruitId($fruitId);

}

FruitsModel.php

<?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
    }

}

Really, that's all the magic. Just force the FruitsModelInterface to implement the setFruitId() method by implementing the proper interface. In your displayFruit() you just check if the property really has been assigned.

I also made your property protected, so that you can be sure the value will just be set from within the class or it's children.

Happy Coding!


Further reading

OTHER TIPS

What you should do is initalize $fruitId to null as default, then you can say: if($this->fruitId === null){}

By the way, make your attributes private, you don't want to have setters with a public variable :)

You can try using method_exits(). Which check if a method exists within a class.

Something like:

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

For more documentation check this link out: http://www.php.net/method_exists

Well you can make a function inside your class.

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

        return $retVal;
    }
}

Usage:

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

creates a new method for the value of the variable, this can be private if you want another class does not have access to it:

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;

    }

}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top