我知道PHP没有纯粹的对象变量,但我想检查给定对象或类中是否存在某个属性。
$ob = (object) array('a' => 1, 'b' => 12);
或者$ob = new stdClass;
$ob->a = 1;
$ob->b = 2;
在JS中,我可以编写以下代码来检查变量a
是否存在于一个对象中:
if ('a' in ob)
在PHP中,是否可以像这样做?
我知道PHP没有纯粹的对象变量,但我想检查给定对象或类中是否存在某个属性。
$ob = (object) array('a' => 1, 'b' => 12);
或者$ob = new stdClass;
$ob->a = 1;
$ob->b = 2;
在JS中,我可以编写以下代码来检查变量a
是否存在于一个对象中:
if ('a' in ob)
在PHP中,是否可以像这样做?
if (property_exists($ob, 'a'))
注意:如果属性为null,则isset()将返回false
if (isset($ob->a))
例子1:
$ob->a = null
var_dump(isset($ob->a)); // false
例子2:
class Foo
{
public $bar = null;
}
$foo = new Foo();
var_dump(property_exists($foo, 'bar')); // true
var_dump(isset($foo->bar)); // false
empty()
更好的情况吗?!empty($var)
? - b_dubbempty()
返回 true。 - Peter要检查属性是否存在且是否为null,您可以使用函数property_exists()
文档:http://php.net/manual/en/function.property-exists.php
与isset()相反,property_exists()即使属性的值为NULL也会返回TRUE。
bool property_exists ( mixed $class , string $property )
示例:
if (property_exists($testObject, $property)) {
//do something
}
isset和property_exists都不能满足我的需求。
最终我选择了:
$exists = array_key_exists($property, get_object_vars($obj));
例子:
class Foo {
public $bar;
function __construct() {
$property = 'bar';
isset($this->$property); // FALSE
property_exists($this, $property); // TRUE
array_key_exists($property, get_object_vars($this)); // TRUE
unset($this->$property);
isset($this->$property); // FALSE
property_exists($this, $property); // TRUE
array_key_exists($property, get_object_vars($this)); // FALSE
$this->$property = 'baz';
isset($this->$property); // TRUE
property_exists($this, $property); // TRUE
array_key_exists($property, get_object_vars($this)); // TRUE
}
}
解决方案
echo $person->middleName ?? 'Person does not have a middle name';
为了更清楚地展示这个语句在if语句中的样子,以便更好地理解它是如何工作的。
if($person->middleName ?? false) {
echo $person->middleName;
} else {
echo 'Person does not have a middle name';
}
说明
传统的PHP检查某个东西是否存在的方法是这样做:
if(isset($person->middleName)) {
echo $person->middleName;
} else {
echo 'Person does not have a middle name';
}
或者更加特定于类的方式:
if(property_exists($person, 'middleName')) {
echo $person->middleName;
} else {
echo 'Person does not have a middle name';
}
这些语句在长表达式中都可以使用,但在三元语句中会变得不必要得繁琐,如下所示:
isset($person->middleName) ? echo $person->middleName : echo 'Person does not have a middle name';
您也可以只使用三元运算符来实现:
echo $person->middleName ?: 'Person does not have a middle name';
但是......如果该值不存在(未设置),它将引发一个E_NOTICE
错误,并且不是最佳实践。如果该值为null
,它将不会引发异常。
因此,三元运算符挺身而出,使这成为一个简洁的答案:
echo $person->middleName ?? 'Person does not have a middle name';
?:
)并不检查属性是否存在!而 ??
是空值合并运算符。 - mickmackusa?:
的内容。 - Rob?:
,因为标题明确说明:PHP检查对象或类中是否存在属性
。 - mickmackusa如果您想知道您定义的类的实例中是否存在某个属性,只需将property_exists()
和isset()
结合使用即可。
public function hasProperty($property)
{
return property_exists($this, $property) && isset($this->$property);
}
property_exists($this, $property)
在这里有点多余,因为你的代码结果始终与仅使用isset($this->$property)
相同。 - MarthyMisset()
无法告诉您属性是否是类定义的真正成员。稍后我会再次查阅以确保。 - Anthony Rutledge__get()
和更重要的 __isset()
魔术方法的虚拟属性,则在某些情况下输出将会不同。 - MarthyMif(isset($obj->a))
{
//do something
}
if (property_exists('class', $property)) {
//do something
}
/**
* @param Object $object
* @param string $property as a string with nested properties 'prop1.nesterdProp.deepvalue'
* @param mixed $default
* @return mixed
*/
function getPropertyOrDefault(Object $object, string $property, $default = null)
{
$value = $object;
$path = explode('.', $property);
foreach ($path as $prop) {
if (is_object($value) && property_exists($value, $prop)) {
$value = $value->{$prop};
} else {
return $default;
}
}
return $value;
}
请记住,如果属性为空,则会得到空值并且没有默认值。
顺便说一下,类似的帮助程序也适用于JS。
我在这里发表一下我的看法。
给定以下类:
class Foo
{
private $data;
public function __construct(array $data)
{
$this->data = $data;
}
public function __get($name)
{
return $data[$name];
}
public function __isset($name)
{
return array_key_exists($name, $this->data);
}
}
接下来会发生以下事情:
$foo = new Foo(['key' => 'value', 'bar' => null]);
var_dump(property_exists($foo, 'key')); // false
var_dump(isset($foo->key)); // true
var_dump(property_exists($foo, 'bar')); // false
var_dump(isset($foo->bar)); // true, although $data['bar'] == null