PHP:如何添加到对象类?

PHP:如何添加到对象类?,php,oop,object,Php,Oop,Object,我刚开始学习PHP中的对象。我有下面的PHP示例供练习。我不知道我的结构设置是否正确。我希望能够添加到代码底部注释部分提到的停止。我已经设置好并进入了这里,但是可能缺少了其他一些东西来访问变量,如echo$obj->DESTINATION或echo$obj->STOPS[0] <?php class EastBound { private $DESTINATION; // Final stop private $STOPS; // Three stops alo

我刚开始学习PHP中的对象。我有下面的PHP示例供练习。我不知道我的结构设置是否正确。我希望能够添加到代码底部注释部分提到的停止。我已经设置好并进入了这里,但是可能缺少了其他一些东西来访问变量,如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"
*/ 

?>

您从未真正调用stops 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']);
}

如果您想精确地添加三个停止点,可以使用三元素数组调用
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("...", "..."));

后者确实是最具面向对象性的。

如果您要编写真正的面向对象程序,您应该使用以下方法:

/**
 * 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)
 */

我看到一个getter和一个setter,你的
add\u stop
函数在哪里?(提示:如果你想添加止损点,添加一个加法器=)@Mike'Pomax'Kamermans OK。我该怎么做?添加其他函数的方法相同
函数add_stop($stop){$this->STOPS[]=$stop;}
,具有相关的可视性语法,所以我仍然需要使用数组来处理对象?或者还有其他方法吗?如果你想提供你想要的功能类型,不,你必须使用数组。但是访问与对象的特定实例相关联的数组是很常见的,就像您尝试执行的操作一样。我认为数组是首选的方式。我想您是指['name']和['time']?是的,我忘记了右引号。是的,我希望能够添加多个停止点。那我怎么称呼他们呢?我可以执行echo$obj->STOPS[0]->NAME吗?没有任何可见性规则,可以。如果我们将STOPS重命名为STOPS,它将是
$obj->STOPS[0]->name
(不要对变量使用所有大写字母,这仅适用于常量)。如果我们想标记变量
private
,那么我们需要使用getter:
$obj->get_stops()[0]->get_name()
Ftr:以下构造仅在PHP5.4+中有效:
$obj->get_stops()[0]->get_name()
这是一个好的观点。在旧版本的PHP中,必须首先将
$obj->get_stops()
缓存为局部变量,即
$stops=$obj->get_stops()然后使用$stops[0]等。这是如何实现“真正的oop”的?无论是存储字符串数组还是对象数组,他仍然需要提供一个成员数组容器来容纳多个条目。诚然,对于未来的功能增强来说,创建一个停止对象要比最终得到一个巨大的多维数组更加灵活。这是你在任何面向对象语言的每一本体面的OOP书中读到的第一件事。创建表示数据的对象。是的,在某些情况下,您可能会使用
数组
,但这取决于案例和您所持有的数据。在这种情况下,他有一个
停止
,它有多个
属性
。因此,我的观点是,使用对象而不是普通的老式数组是一个更明智的想法,OP的基本问题仍然存在。如何访问对象的多个相似属性?答:使用成员数组容器,无论是否存储对象、字符串等。
/**
 * 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)
 */