【发布时间】:2013-05-01 10:28:02
【问题描述】:
我有这样的功能:
// merge - merge two or more given trees and returns the resulting tree
function merge() {
if ($arguments = func_get_args()) {
$count = func_num_args();
// and here goes the tricky part... :P
}
}
我可以使用get_class()、is_*() 甚至ctype_*() 之类的函数检查所有给定参数是否属于同一类型/类(在本例中为类),但它的操作(据我所知)在单个元素级别。
理想情况下,我想做的是类似于in_array() 函数但比较数组中所有元素的类,所以我会做类似in_class($class, $arguments, true) 的事情。
我可以这样做:
$check = true;
foreach ($arguments as $argument) {
$check &= (get_class($argument) === "Helpers\\Structures\\Tree\\Root" ? true : false);
}
if ($check) {
// continue with the function execution
}
所以我的问题是……有为此定义的函数吗?或者,至少,有更好/更优雅的方法来实现这一点?
【问题讨论】:
-
get_object_vars 或 get_class_vars 可能吗? php.net/manual/en/function.get-object-vars.php
-
你到底想做什么?
-
不,我需要获取每个参数的类名并对其进行检查。这两种方法将公开每个参数属性(在我的情况下为
$attributes),但它不起作用:P -
@NullVoid 我想检查是否所有给定的参数都是
Tree类的实例(这是一个合并树的函数)。
标签: php class arguments elements