Evaluate object to a boolean

前端 未结 6 1440
醉话见心
醉话见心 2020-12-17 08:58

Consider the following:

class MyClass
{
  private $var1 = \"apple\";
  private $var2 = \"orange\";
}

$obj = new MyClass();

if($obj) { 
  // do this
}
else          


        
相关标签:
6条回答
  • 2020-12-17 09:40

    PHP evaluates my object to true because it has member variables.

    This is incorrect. PHP actually evaluates $obj as true because it holds an object. It has nothing to do with the contents of the object. You can verify this by removing the members from your class definition, it won't make any difference in which branch of the if/else is chosen.

    There is no way of making PHP evaluate a variable as false if it holds a reference to an object. You'd have to assign something "falsy" to the variable, which includes the following values:

    null
    array()
    ""
    false
    0
    

    See the Converting to boolean from the PHP documentation for a list of all values that are treated as false when converted to a boolean.

    0 讨论(0)
  • 2020-12-17 09:43

    The best you can do is using __invoke:

    class MyObject {
    
        private $_state;
    
        public function __construct($state = false) {
            $this->_state = $state;
        }
    
        public function __invoke() {
            return $this->_state;
        }
    
    }
    
    $true  = new MyObject(true);
    $false = new MyObject(false);
    
    var_dump($true());   // true
    var_dump($false());  // false
    
    0 讨论(0)
  • 2020-12-17 09:49

    Sorry for being late, but I just played with similar task and have made some kind of a hack/workaround. Notice "$object" pattern which calls __toString method.

    class tst {
      public $r = true;
      function __toString() { return $this->r ? "1" : "0"; }
    }
    $object = new tst();
    $object->r = true;
    echo '<br />1: ';
    if ("$object") echo 'true!'; else echo 'false!'; // "$object" becomes "1" == true
    //echo "$object" ? 'true!' : 'false!';
    
    $object->r = false;
    echo '<br />0: ';
    echo "$object" ? 'true!' : 'false!'; // "$object" becomes "0" == false
    

    Output:

    1: true!
    0: false!
    
    0 讨论(0)
  • 2020-12-17 09:50

    You can do it like that : (bool) $yourObject

    will

    0 讨论(0)
  • 2020-12-17 09:58

    You can use small extension for php7: https://github.com/p1ncet/obcast. As you may see from the description, it allows to cast an object to boolean by implementing new internal interface Boolable:

    $obj = new class implements Boolable {
        public $container = [];
        public function __toBoolean() {
            return count($this->container) > 0;
        }
    };
    
    var_dump((bool) $obj);
    
    $obj->container = [1];
    var_dump((bool) $obj);
    

    output:

    bool(false)
    bool(true)
    
    0 讨论(0)
  • 2020-12-17 10:01

    No, you can't. Unfortunately boolean casting in php is not modifiable, and an object will always return true when converted to a boolean.

    Since you clearly have some logic you mean to place in that control statement, why not define a method on your object (say "isValid()" ) that checks the conditions you wish to check, and then replace:

    if ($obj)
    

    with:

    if ($obj->isValid())
    
    0 讨论(0)
提交回复
热议问题