我刚开始学习 PHP 中的对象。我有以下示例 PHP 用于练习。我不知道我的结构设置是否正确。我希望能够添加到代码底部注释部分中提到的 STOPS。我在这里有 SET 和 GET,但可能缺少其他东西来访问 echo $obj->DESTINATION 或 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"
*/
?>
这是输出:
Final destination is Neverland.
object(EastBound)#1 (2) {
["DESTINATION":"EastBound":private]=>
string(9) "Neverland"
["STOPS":"EastBound":private]=>
string(0) ""
}