當我嘗試下面的代碼時,我只得到第一個輸出。我想所有的輸出追加到數組self::$results
使同一函數將返回數組,但在運行腳本後,該函數返回數組,但只有第一個輸出。這意味着,它只附加第一個輸出。如何在同一個函數中爲單個數組添加更多值?
<?php
/**
* @author Ewoenam
* @copyright 2014
*
* @odj class book
*/
class book
{
/**
* @array static $alpha; holds array of accepted words
*/
public static $alpha = array();
/**
* @array static $result; holds array of correct answers
*/
public static $results;
/**
* @string protected $word; parametr
*/
protected $word;
/**
* @var static $alpha; holder of class instance getter
* returns self::getInstance
*/
public static $init;
/**
* @method static getInstance(); class instance getter
* returns self();
*/
function __construct()
{
self::$alpha = array('love','life','health','power','money','God');
}
public static function getInstance()
{
if (self::$init === null)
{
self::$init = new self();
return self::$init;
}
}
/**
* @method static check()
* takes 1 param; self::$word
* returns bool
*/
public static function check($word)
{
for($i=0;$i<=count(self::$alpha)-1;$i++)
{
if(similar_text($word,self::$alpha[$i]) === strlen($word))
{
return true;
}
}
}
/**
* @method static result()
* takes 1 param; self::check()
* returns bool
*/
public static function result($bool)
{
if($bool === true)
{
return 'correct';
}
else
{
return 'wrong';
}
}
/**
* @method static getter()
* takes 1 param; array of words to be searched
* returns array self::$results
*/
public static function getter($array)
{
self::$results = array();
for($i = 0;$i<=count($array)-1;$i++)
{
// i want to add more thn one answers to to $result array but i get only the first answer.
//how do i ddo it?
self::$results[] = self::result(book::check($array[$i]));
return self::$results;
}
}
}
$array = array('love','ama','kofi','money','health','God');
print_r(book::getInstance()->getter($array));
var_dump(book::check('love')) ;
?>
非常感謝。有用 – 2014-10-03 21:38:02