1

If a class constructor sets a property of that class, is it recommended to still check if that property is set in other methods? The code:

class A {

    public $property;

    public function __construct($value) {
        $this->property = $value;
    }

    public function doSomething() {

        if(isset($this->property)) {
            //actual code here
            //...but is this isset-check necessary?
        }
        else { return false; }
    }
} 

I'm just used to check if vars are set, just wondering if there are any concerns leaving it out. Thanks!

joepd
  • 49
  • 1
  • 8

1 Answers1

1

It depends on what you want to achieve.

isset($this->property) in your particular case will be true if the property is null as well. If that's what you want - the better could be to check if it's null explicitly

if ($this->property === null)

If that's not what you want and you just want to ensure the property exists then it's redundant because:

  1. As soon as the property is declared - it will be there
  2. If you're worrying about some code to unset the property since it's public (which is a bad idea anyway) - then make it private or protected and don't expose direct access to it, but provide a getter instead
zerkms
  • 249,484
  • 69
  • 436
  • 539
  • Thanks for the answer. Making the property private seems like a good idea to begin with. All I want is the code to stop running if $property doesn't contain a value of a certain format (single- or multi-dimensional array filled with ints only). These checks are in the constructor in my real code. And the class can't initialize if the constructor returns false, right? So other methods can't execute anything anyway in that case if I'm correct. That was my question. ;) – joepd Mar 26 '14 at 23:34
  • "And the class can't initialize if the constructor returns false, right?" --- not correct. A constructor cannot return any value. The only way to stop initializing the object's state - is to throw an exception from a constructor. – zerkms Mar 26 '14 at 23:36
  • This is what happens if you don't code for a while! ;) I figured that using a setter function is probably much better than putting all that logic in the constructor. However, in my case, the data in $property is absolutely necessary for the class to operate (every method uses $property). It can be argued to set $property in the constructor, as is said on [this page](http://stackoverflow.com/questions/3032808/purpose-of-php-constructors). I just don't like all those throw/try/catch stuff in my code, so would it be better to use a setter? – joepd Mar 26 '14 at 23:55
  • @joepd: it depends. The constructor is guaranteed to run, setter - isn't. If you are fine that your object can live not completely initialized (someone (un)intentionally didn't call a setter) - then that's a solution. I wouldn't do that though. – zerkms Mar 27 '14 at 00:06
  • I don't know old 2014 php, but isset($this->property) returns false when $property is null. Am I missing something? And here, I always use this table when I am confused. It is very helpful: https://www.virendrachandak.com/techtalk/php-isset-vs-empty-vs-is_null/ – Onur Demir May 09 '20 at 08:38