如果$output
是字符串,下面的方法非常有效。尽管在极少数情况下,CCD_ 2可以作为数组传递。
如何检查$output
是否是数组,并且只获取数组的最后一部分。
/**
* Output writer.
*
* @param string $output
* @param Controller $oController
* @throws Output_Exception
*/
public static function factory($output,Controller $oController) {
$outtype = ucfirst(strtolower(str_replace(".","_",$output)));
$classname = __CLASS__ . "_" . $outtype;
try {
Zend_Loader::loadClass($classname);
$oOutputter = new $classname($oController);
if(! $oOutputter instanceof Output_Abstract )
throw new Output_Exception("class $classname is not an instance of Output_Abstract");
} catch (Zend_Exception $e) {
throw $e;
}
return $oOutputter;
}
错误:
警告:strtolower()要求参数1为字符串,中给定的数组C: 第16行上的''wamp''www''cms''webapp''library''Output.php
警告:include_one(Output.php)[function.include once]:无法打开流:中没有这样的文件或目录C: 第146行上的''wamp''php''includes''Zend''Loader.php
var_dump($output)
array(2) { [0]=> string(6) "xml" [1]=> string(6) "smarty" }
if ( is_array($output) ) {
$output= end($output);
}
请参阅
http://docs.php.net/is_array
http://docs.php.net/function.end
您应该能够使用PHP的is_array()函数。下面这样的东西应该可以做到。
<?php
//...
$output = is_array($output) ? $output[count($output)-1] : $output;
//...
?>
此代码段将检查$output是否为数组,如果是,则将$output设置为数组中的最后一个元素。否则,它将保持不变。
您可以在以下链接中阅读更多关于is_array()的信息:
http://www.php.net/manual/en/function.is-array.php
编辑:看起来有人打败了我,但我建议不要在这里使用end()函数。如果调用函数的代码期望数组未被修改,那么这样做可能会产生意外的结果。
尝试更改此项:
$outtype = ucfirst(strtolower(str_replace(".","_",$output)));
到此:
if(is_array($output))
$outout= $output[count($outout)-1];
$outtype = ucfirst(strtolower(str_replace(".","_",$output)));