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

実際に停止セッターアクセサーを呼び出すことはありません。これを試してください:

 $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 に答える
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 に答える