0

我刚开始学习 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) ""
}
4

3 回答 3

3

你从来没有真正调用过 stop setter 访问器,试试这个:

 $obj = new EastBound();

 $obj->set_STOPS(
     array(
         array(
             'name' => 'Radio City'
             'time' => '6/16/2013 8:28:00 PM'
         ),
         array(
             'name' => 'Malt Shoppe Street'
             'time' => '6/16/2013 8:28:00 PM'
         ),
         array(
             'name' => 'Neverland'
             'time' => '6/16/2013 8:28:00 PM'
         )
    )
);

允许对象访问多个相关“属性”的能力是很常见的。为此,您必须使用数组。对于您的具体示例,在设置停靠点后,您可以稍后通过以下方式逐步完成每个停靠点:

foreach ($obj->get_STOPS() as $key => $stop) {

    // Will output: Stop #1: Radio City @ 6/16/2013 8:28:00 PM
    echo sprintf('Stop #%d: %s @ %s', ($key +1), $stop['name'], $stop['time']);
}
于 2013-06-18T23:00:21.027 回答
3

如果要精确添加三个停靠点,可以set_stops使用三元素数组调用。但是,这不是很有用。相反,只需添加一个 add_stop() 函数:

function add_stop($stop) {
  $this->STOPS[] = $stop;
}

然后在您的实际脚本中,添加三个站点:

$obj->add_stop(array('name' => 'Radio City', 'time' => '...'));
$obj->add_stop(array('name' => 'Malt Shoppe Street', 'time' => '...'));
$obj->add_stop(array('name' => '...', 'time' => '...'));

事实上,现在我们可以根据需要添加任意数量的站点。三、七、五百……发疯!

也就是说,其他一些人也指出 PHP 代码有某些代码样式约定,而此代码不遵循这些约定,使得日复一日阅读 PHP 的人更难阅读您的代码并为您提供帮助. 将这些全大写变量转换为小写,并尝试使您的函数也保持小写。

如果你真的想要,你也可以让 Stop 类,以正确的OOP 风格做事:

class Stop {
  var $name;
  var $time;
  function __construct($name, $time) {
    $this->name = $name;
    $this->time = $time;
  }
  function get_name() { return $this->name; }
  function get_time() { return $this->time; }
}

使用带有名称/时间参数的 add_stop:

$obj->add_stop($name, $time) {
  $this->stops[] = new Stop($name, $time);
}

或采取停止对象:

$obj->add_stop(new Stop("...", "..."));

后者是最 OOP 的,真的。

于 2013-06-18T23:03:11.257 回答
2

如果您要编写真正的 OOP,您应该使用这种方法:

/**
 * Class Stop
 * This is an object which represents the "Stop"
 */
class Stop {
    private $name;
    private $time;

    public function __construct($name = '', $time = ''){
        $this->name = $name;
        $this->time = $time;
    }

    /**
     * @param string $name
     */
    public function setName($name) {
        $this->name = $name;
    }

    /**
     * @return string
     */
    public function getName() {
        return $this->name;
    }

    /**
     * @param string $time
     */
    public function setTime($time) {
        $this->time = $time;
    }

    /**
     * @return string
     */
    public function getTime() {
        return $this->time;
    }
}

class EastBound
{
    /**
     * @var Stop
     */
    private $destination;       // Final stop
    private $stops = array();   // All stops, excluding the final destination.

    /**
     * Constructor
     */
    public function __construct(){ }

    /**
     * Get destination
     * @return string $this->destination
     */
    public function getDestination(){
        return $this->destination;
    }

    /**
     * Set destination
     *
     * @param \Stop $destination
     */
    public function setDestination( Stop $destination ) {
        $this->destination = $destination;
    }

    public function addStop( Stop $stop ) {
        $this->stops[] = $stop;
    }

    /**
     * If you already have a "stop" list with all of the stops, you can define it using this function
     * @param array $stops
     */
    public function setStops( array $stops ){
        $this->stops = $stops;
    }

    /**
     * Get all of the stops, including the destination
     * @return array
     */
    public function getStops() {
        return array_merge($this->stops, array($this->destination));
    }

}

$obj = new EastBound();
$obj->setDestination(new Stop('Neverland', '6/16/2013 8:36:00 PM'));
$obj->addStop(new Stop('Radio City', '6/16/2013 8:28:00 PM'));
$obj->addStop(new Stop('Malt Shoppe Street', '6/16/2013 8:31:30 PM'));
$dest = $obj->getDestination();
echo "Final destination is ".$dest->getName(). ".\n";
var_dump($obj->getStops());

/**
 * Will return:
    Final destination is Neverland.
    array (size=3)
        0 =>
            object(Stop)[3]
                private 'name' => string 'Radio City' (length=10)
                private 'time' => string '6/16/2013 8:28:00 PM' (length=20)
        1 =>
            object(Stop)[4]
                private 'name' => string 'Malt Shoppe Street' (length=18)
                private 'time' => string '6/16/2013 8:31:30 PM' (length=20)
        2 =>
            object(Stop)[2]
                private 'name' => string 'Neverland' (length=9)
                private 'time' => string '6/16/2013 8:36:00 PM' (length=20)
 */
于 2013-06-18T23:11:24.243 回答