I just started to learn about Objects in PHP. I have the following example PHP for practice. I don't know if I have the structure set-up correctly or not. I want to be able to add to STOPS as mentioned in the commented section at the bottom of the code. I have the SET and GET in here, but maybe something else is missing to access variables like echo $obj->DESTINATION or echo $obj->STOPS[0].
<?php
class EastBound
{
private $DESTINATION; // Final stop
private $STOPS; // Three stops along the way to the final stop.
public function __construct(){
$this->DESTINATION = '';
$this->STOPS = '';
}
/* GETTERS */
public function get_DESTINATION(){
return $this->DESTINATION;
}
public function get_STOPS(){
return $this->STOPS;
}
/* SETTERS */
public function set_DESTINATION($data){
$this->DESTINATION = $data;
}
public function set_STOPS($data){
$this->STOPS = $data;
}
}
$obj = new EastBound();
$obj->set_DESTINATION("Neverland");
$dest = $obj->get_DESTINATION();
echo "Final destination is $dest." . "\n";
var_dump($obj);
/* How do I add these three stops to STOPS?
For example:
STOP[0]
NAME "Radio City"
TIME "6/16/2013 8:28:00 PM"
STOP[1]
NAME "Malt Shoppe Street"
TIME "6/16/2013 8:31:30 PM"
STOP[2]
NAME "Neverland"
TIME "6/16/2013 8:36:00 PM"
*/
?>
Here is the output:
Final destination is Neverland.
object(EastBound)#1 (2) {
["DESTINATION":"EastBound":private]=>
string(9) "Neverland"
["STOPS":"EastBound":private]=>
string(0) ""
}
add_stopfunction? (hint: if you want to add stops, add an adder =)function add_stop($stop) { $this->STOPS[] = $stop; }, with the relevant visbility syntax