PHP is_callable with Type Definitions(PHP is_callable with Type Definitions)
一直在搜索PHP文档,这似乎不可能,但想检查。
说我有这样的功能:
class Utils { static function doSomething( Array $input ){ ... } }
是否可以使用像
is_callable
这样的内置PHP函数来检查函数是否存在以及函数中的类型定义是否接受我所拥有的变量。所以:
$varA = array( 'a', 'b', 'c' ); $varB = 'some string'; $functionToCall = array( 'Utils', 'doSomething' ); is_callable( $functionToCall, $varA ) => true; is_callable( $functionToCall, $varB ) => false;
当然
is_callable
不能像这样使用。 但是可以在不使用Try Catch的情况下完成吗?如果不是这将是最好的方式吗?
try { Utils::doSomething( 10 ) } catch (TypeError $e) { // react here }
谢谢!
Been searching the PHP documentation and this doesn't seem possible but wanted to check.
Say I have a function like this:
class Utils { static function doSomething( Array $input ){ ... } }
Is is possible to use a inbuilt PHP function like
is_callable
to check if both the function exists and if the variable I have will be accepted by the type definition in the function.So:
$varA = array( 'a', 'b', 'c' ); $varB = 'some string'; $functionToCall = array( 'Utils', 'doSomething' ); is_callable( $functionToCall, $varA ) => true; is_callable( $functionToCall, $varB ) => false;
Of course
is_callable
cannot be used like this. But can it be done without using a Try Catch?If not would this be the best way around it?
try { Utils::doSomething( 10 ) } catch (TypeError $e) { // react here }
Thanks!
原文:https://stackoverflow.com/questions/38916981
最满意答案
我们可以使用
replace
逻辑矩阵作为第二个参数,替换值作为第三个replace(mat, mat < 0, 0) # [,1] [,2] [,3] [,4] [,5] [,6] #[1,] 0 0 0 0 1 0 #[2,] 0 0 0 0 2 1 #[3,] 0 0 0 0 1 2 #[4,] 0 0 0 0 0 1 #[5,] 1 2 1 0 0 0 #[6,] 0 1 2 1 0 0
或者通过基于逻辑矩阵提取'mat'的值并将其分配给0来使用分配
mat[mat < 0] <- 0
由于它是一个数字
matrix
,所以算术也应该起作用(mat >= 0) * mat
We can either use
replace
with the logical matrix as the second parameter and replacement value as the thirdreplace(mat, mat < 0, 0) # [,1] [,2] [,3] [,4] [,5] [,6] #[1,] 0 0 0 0 1 0 #[2,] 0 0 0 0 2 1 #[3,] 0 0 0 0 1 2 #[4,] 0 0 0 0 0 1 #[5,] 1 2 1 0 0 0 #[6,] 0 1 2 1 0 0
or use the assignment by extracting the values of the 'mat' based on the logical matrix and assigning it to 0
mat[mat < 0] <- 0
As it s a numeric
matrix
, arithmetic should also work(mat >= 0) * mat
相关问答
更多-
在Matlab中根据它们的值和索引替换矩阵条目(Replace matrix entries in Matlab based on their value and their index)[2022-11-08]
更通用的一点: R2016b之前的MATLAB: B = bsxfun(@eq, A, (1:size(A,1)).'); MATLAB R2016b及更高版本: B = ( A == (1:size(A,1)).' ); Bit more generic: MATLAB before R2016b: B = bsxfun(@eq, A, (1:size(A,1)).'); MATLAB R2016b and later: B = ( A == (1:size(A,1)).' ); -
我怀疑你想要outer : > mat <- matrix(NA, nrow=5, ncol=3) > outer(1:nrow(mat), 1:ncol(mat) , FUN="*") [,1] [,2] [,3] [1,] 1 2 3 [2,] 2 4 6 [3,] 3 6 9 [4,] 4 8 12 [5,] 5 10 15 > outer(1:nrow(mat), 1:ncol(mat) , FUN=f ...
-
matrix( m2[ match(m1, rownames(m2) )], ncol=2) [,1] [,2] [1,] 0 0 [2,] 0 0 [3,] 1 1 [4,] 0 1 m3 <- matrix(m2[match(m1, rownames(m2) )], ncol=2) which( m3[,1]==0 & m3[,2]==1 ) #[1] 4 m3[,1]==0 & m3[,2]==1 # [1] FALSE FALS ...
-
如何根据另一个矩阵的真/假索引来替换矩阵中的值?(How to replace values in matrix based on true/false indices of another matrix?)[2023-11-09]
我们可以使用replace逻辑矩阵作为第二个参数,替换值作为第三个 replace(mat, mat < 0, 0) # [,1] [,2] [,3] [,4] [,5] [,6] #[1,] 0 0 0 0 1 0 #[2,] 0 0 0 0 2 1 #[3,] 0 0 0 0 1 2 #[4,] 0 0 0 0 0 1 #[5,] 1 2 ... -
特定 a = [1, 0, 0, 1]; b = [1, 2, 3]; c = [3, 4, 5]; 让我们首先在最终矩阵中获取我们想要的数组并将它们放在一个单元数组中: parts = {b, c} parts = { [1,1] = 1 2 3 [1,2] = 3 4 5 } 目标是将a作为索引的值用于parts ,但要做到这一点,我们需要从1到n所有值为n (如果有缺失值,则需要更多的工作)。 在这种情况下,我们可以增加a : a_inds = a + ...
-
如果将矩阵转换为向量,则可以看得更清楚,矩阵元素按列索引,如果通过单个索引访问矩阵,则可以找到该值,例如, largeMatrix[1]给出1 , largeMatrix[2]给出3等等: as.vector(largeMatrix) # [1] 1 3 5 7 9 11 2 4 6 8 10 12 as.vector(upperTriangleSmall) # [1] FALSE FALSE TRUE FALSE 所以它的作用是为每个四个元素的块拾取第三个元素,因为两个矩阵的长度不 ...
-
您可以使用统计工具箱中的GRPSTATS功能: val = [5,9,2 ; 3,4,1 ; 6,8,7]; idx = [1,3,1 ; 2,4,2 ; 1,3,1]; result = grpstats(val(:),idx(:),'sum'); result = reshape(result, 2, 2); You can use GRPSTATS function from Statistical Toolbox: val = [5,9,2 ; 3,4,1 ; 6,8,7]; idx = [1, ...
-
Python在矩阵中查找数组的索引并给出不同的值(Python find indices of an array in a matrix and give different value)[2021-08-09]
对于完全矢量化的解决方案,您可以使用np.in1d和np.searchsorted的组合: np.searchsorted(a, b[~np.any(~np.in1d(b, a).reshape(b.shape), axis=1), :]) For a fully vectorized solution you can use a combination of np.in1d and np.searchsorted: np.searchsorted(a, b[~np.any(~np.in1d(b, a). ... -
使用ismember的第二个输出为您提供A的第1行中的值的索引。使用这些索引直接创建矩阵SS 。 示例(为清晰起见,更改了初始值): S = [5 5 5 3 3 3; 5 5 5 3 3 3; 3 3 3 3 5 5; 3 3 3 3 5 5; 3 3 3 3 5 5]; A = [5 3; 2 4]; >> [~, Locb] = ismember(S,A(1,:)) Locb = 1 1 1 2 2 2 1 1 1 ...
-
我提取了非零元素的索引,并使用ave()来计算分组排名 idx <- which(TestMatrix != 0, arr.ind=TRUE) ranks = ave(-TestMatrix[idx], idx[,2], FUN=rank) 或者实际上是你想要的结果,要保留的价值 keep = ave(-TestMatrix[idx], idx[,2], FUN=function(elt) { elt = rank(elt) (elt > 2) & (elt <= length(elt) ...