I want to execute some functions if entity is member of few classes but not some.
如果实体是少数类的成员,而不是某些类,那么我想执行一些函数。
There is a function called instanceof
.
有一个函数叫instanceof。
But is there something like
但是否有类似的东西。
if ($entity !instanceof [User,Order,Product])
5 个解决方案
#1
89
Give them a common interface and then
给他们一个通用接口。
if (!$entity instanceof ShopEntity)
or stay with
或者留在
if (!$entity instanceof User && !$entity instanceof Product && !$entity instanceof Order)
I would avoid creating arbitrary functions just to save some characters at a single place. On the other side if you need it "too often", you may have a design flaw? (In the meaning of "too much edge cases" or such)
我将避免创建任意函数,只是为了在一个地方保存一些字符。另一方面,如果你“经常”需要它,你可能有设计缺陷?(“太多边缘情况”的意思)
#2
44
PHP manual says: http://php.net/manual/en/language.operators.type.php
PHP手册说:http://php.net/manual/en/language.operators.type.php
!($a instanceof stdClass)
This is just a logical and "grammatically" correct written syntax.
这只是一种逻辑上和语法上正确的书写语法。
The above suggested syntax though, is tricky because we are not specifying which exactly is the scope of the negation: the variable itself or the whole construct of "$var instanceof someclass". We will only have to rely here on the operator precendence [Edited, thanks to @Kolyunya].
上面提到的语法很棘手,因为我们没有指定否定的范围:变量本身或“$var instanceof someclass”的整个构造。在这里,我们只需要依靠运营商的优势[经过编辑,感谢@Kolyunya]。
!$class instanceof someClass
#3
10
PHP运算符优先级
instanceof
operator is just before negation then this expression:
实例运算符是在否定之前,那么这个表达式是:
!$class instanceof someClass
is just right in PHP and this do that you expect.
在PHP中是正确的,这是你所期望的。
#4
1
This function should do it:
这个函数应该这样做:
function isInstanceOf($object, Array $classnames) {
foreach($classnames as $classname) {
if($object instanceof $classname){
return true;
}
}
return false;
}
So your code is
所以你的代码是
if (!isInstanceOf($entity, array('User','Order','Product')));
#5
1
function check($object) {
$deciedClasses = [
'UserNameSpace\User',
'OrderNameSpace\Order',
'ProductNameSpace\Product',
];
return (!in_array(get_class($object), $allowedClasses));
}
#1
89
Give them a common interface and then
给他们一个通用接口。
if (!$entity instanceof ShopEntity)
or stay with
或者留在
if (!$entity instanceof User && !$entity instanceof Product && !$entity instanceof Order)
I would avoid creating arbitrary functions just to save some characters at a single place. On the other side if you need it "too often", you may have a design flaw? (In the meaning of "too much edge cases" or such)
我将避免创建任意函数,只是为了在一个地方保存一些字符。另一方面,如果你“经常”需要它,你可能有设计缺陷?(“太多边缘情况”的意思)
#2
44
PHP manual says: http://php.net/manual/en/language.operators.type.php
PHP手册说:http://php.net/manual/en/language.operators.type.php
!($a instanceof stdClass)
This is just a logical and "grammatically" correct written syntax.
这只是一种逻辑上和语法上正确的书写语法。
The above suggested syntax though, is tricky because we are not specifying which exactly is the scope of the negation: the variable itself or the whole construct of "$var instanceof someclass". We will only have to rely here on the operator precendence [Edited, thanks to @Kolyunya].
上面提到的语法很棘手,因为我们没有指定否定的范围:变量本身或“$var instanceof someclass”的整个构造。在这里,我们只需要依靠运营商的优势[经过编辑,感谢@Kolyunya]。
!$class instanceof someClass
#3
10
PHP运算符优先级
instanceof
operator is just before negation then this expression:
实例运算符是在否定之前,那么这个表达式是:
!$class instanceof someClass
is just right in PHP and this do that you expect.
在PHP中是正确的,这是你所期望的。
#4
1
This function should do it:
这个函数应该这样做:
function isInstanceOf($object, Array $classnames) {
foreach($classnames as $classname) {
if($object instanceof $classname){
return true;
}
}
return false;
}
So your code is
所以你的代码是
if (!isInstanceOf($entity, array('User','Order','Product')));
#5
1
function check($object) {
$deciedClasses = [
'UserNameSpace\User',
'OrderNameSpace\Order',
'ProductNameSpace\Product',
];
return (!in_array(get_class($object), $allowedClasses));
}