update page now

Voting

: min(five, zero)?
(Example: nine)

The Note You're Voting On

your dot brother dot t at hotmail dot com
10 years ago
(experienced in PHP 5.6.3) The `empty()` can't evaluate `__get()` results explicitly, so the `empty()` statement bellow always renders true
<?php
class Juice extends Liquid{
 protected $apple;
 protected $orange;
 public function __get($name) {
 return $this->$name;
 }
 public function __construct($apple, $orange) {
 $this->apple = $apple;
 $this->orange = $orange;
 }
}
class Glass {
 protected $liquid;
 public function __get($name) {
 return $name == "liquid" ? $this->liquid : false;
 }
 public function __construct() {
 $this->juice = new Juice(3, 5);
 }
}
$glass = new Glass();
var_dump(empty($this->liquid->apple));
/**
 * The output is:
 * bool(true)
 */
?>

The correct way is to force the evaluation of `__get()` first, by using extra braces around implicit statements like this:
<?php
var_dump(empty(($this->liquid->apple)));
/**
 * The output is:
 * bool(false)
 */
?>

So if you are using packages that utilize object oriented designs and magic methods like `__get()`, it's a good practice to always use double braces for `empty()` calls.

<< Back to user notes page

AltStyle によって変換されたページ (->オリジナル) /