繁体   English   中英

学习PHP oop-不能反向数组

[英]learning php oop - can't reverse array

我正在学习PHP OOP,我正在尝试了解以下脚本为何不起作用:

class ShowTimeline {
    var $conn;
    var $rev;

    function getTimeline ($conn) {
        return $this->conn;
    }

    function reverseTimeline () {
        $rev = array_reverse($this->conn, false);
        return $rev;
    }

    function display () {
        $this->reverseTimeline();
        print_r($this->rev);
    }
}
print '<hr />';
$connect = new showTimeline();
$connect->conn = array('one', 'two', 'three');
$connect->display();

当我将脚本更改为:

//same stuff above
function display () {
            $this->reverseTimeline();
            print_r($this->conn); //changed from $this->rev
        }
//same stuff below

我打印出来:

Array ( [0] => one [1] => two [2] => three )

哪个是对的。 请帮忙?

使用$this->访问类的参数。

function reverseTimeline () {
    $this->rev = array_reverse($this->conn, false);
    return $this->rev;
}

仅使用$rev会将其视为局部变量。

分配给$rev ,实际上是分配给局部变量,而不是对象中的$rev $this->rev永远不会设置。

$rev更改为$this->rev ,事情应该开始起作用。

您实际上从未将$ this-> rev设置为反向数组。

function reverseTimeline () {
            $rev = array_reverse($this->conn, false);
            $this->rev = $rev;
       }

function display () {
            $this->reverseTimeline();
            print_r($this->rev);
        }

会做到的。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM