I'm learning PHP OOP and Im trying to understand why the following script doesn't work:
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();
When I change the script to:
//same stuff above
function display () {
$this->reverseTimeline();
print_r($this->conn); //changed from $this->rev
}
//same stuff below
I get the print out:
Array ( [0] => one [1] => two [2] => three )
which is correct. Please help?
Access class' parameters with $this->
.
function reverseTimeline () {
$this->rev = array_reverse($this->conn, false);
return $this->rev;
}
Using just $rev
it is treated as a local variable.
When you assign to $rev
, you're actually assigning to a local variable, not the $rev
in your object. $this->rev
never gets set.
Change your $rev
s to $this->rev
, and things should start working.
You never actually set $this->rev to the reversed array.
function reverseTimeline () {
$rev = array_reverse($this->conn, false);
$this->rev = $rev;
}
function display () {
$this->reverseTimeline();
print_r($this->rev);
}
would do it.