PHP-检查是否显式设置了类成员


PHP - Check if a class member is explicitly set or not

如果我检查isset($var),我将无法区分以下两种情况。在第一个例子中,我明确地将$t1->a设置为null,而在第二个例子中则保持不变。

<?php
class Test {
 public $a;
}
$t1 = new Test();
$t1->a = null;
if(isExplicitlySet($t1->a)) {
  echo "yes t1'n";
}
$t2 = new Test();
if(isExplicitlySet($t2->a)) {
  echo "yes t2'n";
}

function isExplicitlySet($var) {
//what goes here?
}

编辑:我需要这个功能的原因是:在将Test类的对象持久化到数据库之前,我需要知道$a是显式设置为null还是保持不变。在后面的情况下,我会将其设置为表定义中指定的默认DB值。

好吧,既然您正在编写自己的ORM,那么使用神奇的方法(正如Machavity所建议的)可能是有意义的。你可以创建一个父类

abstract class DB_Obj {
    protected $attributes = array();
    protected $attributes_have_been_set = array();
    public function __set($name, $value) {
        $this->attributes[$name] = $value;
        $this->attributes_have_been_set[$name] = TRUE;
    }
    public function __get($name) {
        return $this->attributes[$name];
    }
    public function __isset($name) {
        return array_key_exists($name, $this->attributes_have_been_set);
    }
}

和CCD_ 1位

class Test extends DB_Obj {
    protected $attributes = array(
        'a' => NULL
    );
}

当你现在这样测试它时,它可以正常工作

$t1 = new Test();
$t1->a = null;
$t2 = new Test();
var_dump( isset($t1->a), isset($t2->a) );
//        bool(true)     bool(false)

这样做的好处还在于,当您想将其保存到数据库时,您不需要知道每个属性的名称(或使用另一个函数),只需在$attributes数组上迭代即可。

您可以在此处看到答案检查值是否为isset和空

通过使用get_defined_vars

$foo = NULL;
$vars = get_defined_vars();
if (array_key_exists('bar', $vars)) {}; // Should evaluate to FALSE
if (array_key_exists('foo', $vars)) {}; // Should evaluate to TRUE

我会亲自创建一个名为UntouchedProperty的类,并在实例化时为其设置属性。然后,原封不动和设置为null将有所不同。

class UntouchedProperty {}
class Foo
{
    public $bar;
    public function __construct()
    {
        $this->bar = new UntouchedProperty;
    }
    public function wasTouched($property)
    {
        if ($this->$property instanceof 'UntouchedProperty') {
            return false;
        }
        return true;
    }
}
$foo = new Foo;
$foo->wasTouched('bar'); #=> false
$foo->bar = null;
$foo->wasTouched('bar'); #=> true