【问题标题】:OOP Basics - generating a random q&aOOP 基础 - 生成随机问答
【发布时间】:2013-07-09 19:58:12
【问题描述】:

我正在尝试创建一个为表单验证目的创建随机安全问题的函数。不过我很挣扎,因为我似乎无法访问变量的最终值。 php.net 以外的任何帮助将不胜感激,我去过那里,但我不是 100% 了解如何创建我的第一个对象。

class question {

public $sq = '';
public $answer = '';

function generate_question() {

    $questions = array( array( question => "What color is the sky?",
                                answer => "blue"),
                        array(question => "What month is after April?",
                                answer => "May"),
                        array(question => "What animal is usually chasen by a cat?",
                                answer => "mouse"),
                        array(question => "What color is banana?",
                                answer => "yellow"),
                        array(question => "What is a next day after Monday?",
                                answer => "Tuesday"),
                        );

    $question = array_rand($questions);

    return $sq = $questions[$question]['question'];
    return $answer = $questions[$question]['answer'];
        }

}
$sqo = new question();

echo $sqo->sq . $sqo->answer;

【问题讨论】:

  • 返回数组和一对问答,同样在实例化对象之后你可以访问方法,而不是它的属性,如果你需要访问属性,你需要使用构造函数和 $this->属性,因为在方法中使用 $var 不会在类中使用公共 $var

标签: php arrays function oop object


【解决方案1】:

方法只能返回一个值,另一个返回值将无法达到。

更改此代码

$question = array_rand($questions);

return $sq = $questions[$question]['question'];
return $answer = $questions[$question]['answer'];

$question = array_rand($questions);
return $questions[$question];

它会起作用的。

要访问返回数组,请使用

echo $sqo->answer['question'];
echo $sqo->answer['answer'];

好的做法是让我们用方法来访问类变量。这些变量应该被声明为私有的并且访问它们的方法应该是公共的。此外,您应该将带有问题答案的数组声明为私有对象变量。不需要每次调用方法时都声明。

我已经重新设计了你的课程以更好(不是最好的)解决方案。

class Question
{

    private $questions;

    public function __construct()
    {
      $this->questions = array( array( question => "What color is the sky?",
                            answer => "blue"),
                    array(question => "What month is after April?",
                            answer => "May"),
                    array(question => "What animal is usually chasen by a cat?",
                            answer => "mouse"),
                    array(question => "What color is banana?",
                            answer => "yellow"),
                    array(question => "What is a next day after Monday?",
                            answer => "Tuesday")
                    );
    }

    public function generate_question()
    {
        return $this->questions[rand(0, count($this->questions)-1)];
    }

}

$sqo = new Question();

$question = $sqo->generate_question();
echo $question['question'];
echo $question['answer'];

编辑和工作

【讨论】:

  • 您好罗伯特,感谢您的回复。返回行中缺少右括号。不过修复后就可以了。非常感谢朋友
  • 实际上,经过几次测试,结果证明该函数每隔几次就会给出一些空结果......为什么会这样,你能做些什么来解决它?
  • 我的错误应该是count($this->questions)-1我会编辑
猜你喜欢
  • 2016-05-28
  • 1970-01-01
  • 2015-07-07
  • 1970-01-01
  • 2011-07-05
  • 2014-06-19
  • 2012-05-28
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多