首页 文章

PHP OOP不在类中传递变量

提问于
浏览
0

我有一个PHP类,用于获取mysqli查询,创建一个多维数组,并且当前只是回显整个数组 . 这是代码:

include("connect.php");

class database
{
    public $motto;
    public $motto_array = array();
    public $rating;
    public $rating_array = array();
    public $category;
    public $score;
    private $mysqli;
    public $counter_array = array();
    public $multi_dim_values = array();
    public $multi_dim_category = array();


    function setMysqli($mysqli)
    {
        $this->mysqli = $mysqli;
    }

    function setCategory($category)
    {
        $this->category = $category;
    }

    function query_category()
    {
        if ($stmt = $this->mysqli->prepare("SELECT motto, score FROM mottos WHERE category=? ORDER BY score DESC"))
        {
            $stmt->bind_param("s", $this->category);
            $stmt->execute();
            $stmt->bind_result($motto, $ranking);
            while ( $stmt->fetch() ) {
                $this->motto_array[] = $motto;
                $this->rating_array[] = $ranking;
            }
            $stmt->close();
        }
    }

    function multi_dim_array()
    {
        $multi_dim_values = array($this->motto_array, $this->rating_array);
        $counter_array = range(0,count($this->motto_array)-1);
        foreach($counter_array as $index => $key) {
            $foreach_array = array();
            foreach($multi_dim_values as $value) {
                $foreach_array[] = $value[$index];
            }
            $multi_dim_category[$key]  = $foreach_array;
        }
        return $multi_dim_category;
    }


}

$class = new database;
$class->SetMysqli($mysqli);
$class->SetCategory("person");
$class->query_category();
print_r($class->multi_dim_array);
print_r($class->multi_dim_category);

connect.php具有mysqli的数据库连接信息 .

我正在学习OOP,所以我在程序中做了这个,并且它运行正常,使用以下代码:

include("connect.php");

    function category($mysqli, $cat)
    {
        if ($stmt = $mysqli->prepare("SELECT motto, score FROM mottos WHERE category=? ORDER BY score DESC"))
        {

        $stmt->bind_param("s", $cat);

        $stmt->execute();

        while($stmt->fetch())
        {
            printf ("[%s (%s) in %s] \n", $motto, $ranking, $category);
            $array .= compact("motto", "category", "ranking");
        }
        print_r($array);*/

        $a = array();
        $b = array();
        $c = array();
        $stmt->bind_result($motto, $ranking);
        while ( $stmt->fetch() ) {
            $a[] = $motto;
            $b[] = $ranking;
        }
        $result = array();
        $values = array($a, $b);
        $c = range(0,count($a)-1);

        foreach($c as $index => $key) {
            $t = array();
            foreach($values as $value) {
                $t[] = $value[$index];
            }
            $result[$key]  = $t;
        }

        return $result;

        $stmt->close();

        }
    }
    $cat = "person";
    $array_one = category($mysqli, $cat);
    print_r($array_one);

这就像我想要的那样打印多维数组 .
我在OOP代码中做错了什么?

谢谢 .

1 回答

  • 3

    你的代码:

    print_r($class->multi_dim_array);
    

    你忘了 () ,所以你're not invoking the method. You'正在访问一个(不存在的)属性 . 试试这个:

    print_r($class->multi_dim_array());
    

相关问题