Php OOP基础知识-生成随机q&;A.

Php OOP基础知识-生成随机q&;A.,php,arrays,function,oop,object,Php,Arrays,Function,Oop,Object,我试图创建一个函数,该函数创建一个随机安全问题,用于表单验证。但是我很挣扎,因为我似乎无法访问变量的最终值。除了php.net之外的任何帮助都将不胜感激,我曾经去过那里,但我不完全理解如何创建我的第一个对象 class question { public $sq = ''; public $answer = ''; function generate_question() { $questions = array( array( question => "What color

我试图创建一个函数,该函数创建一个随机安全问题,用于表单验证。但是我很挣扎,因为我似乎无法访问变量的最终值。除了php.net之外的任何帮助都将不胜感激,我曾经去过那里,但我不完全理解如何创建我的第一个对象

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;

方法只能返回一个值,而无法返回另一个值

更改此代码

$question = array_rand($questions);

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

它会起作用的

要访问返回数组,请使用

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'];

编辑和工作

返回带配对问题答案的数组,同样在实例化对象后,您可以访问该方法,而不是其属性,如果需要访问属性,则需要使用构造函数和$this->属性预先保存它们,因为在方法中使用$var不会在类中使用公共$var,谢谢你的回复。回油管中缺少闭合支架。但它在修复后仍然有效。非常感谢。事实上,经过几次测试,该函数每隔几次就会给出一些空结果。。。为什么会这样?你能做些什么来修复它?我的错误应该是
count($this->questions)-1
我会编辑
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'];