douhao2548 2013-05-06 20:28
浏览 12
已采纳

__toString - 它在这个类中做了什么?

Most of the work I do is on a legacy app written procedurally. I'm beginning to convert pieces of it to OOP, and came across this example class used to add multiple times together:

At the bottom of the class, there is a public function called __toString(), which appears to return some formatted text.

class Duration 
{
    /*
        $d1 = Duration::fromString('2:22');
        $d1->add(Duration::fromString('3:33'));
        echo $d1; // should print 5:55
    */
    public static function fromString($string)
    {
        $parts = explode(':', $string);
        $object = new self();
        if (count($parts) === 2) {
            $object->minutes = $parts[0];
            $object->seconds = $parts[1];
        } elseif (count($parts) === 3) {
            $object->hours = $parts[0];
            $object->minutes = $parts[1];
            $object->seconds = $parts[2];
        } else {
            // handle error
        }
        return $object;
    }

    private $hours;
    private $minutes;
    private $seconds;

    public function getHours() {
        return $this->hours;
    }

    public function getMinutes() {
        return $this->minutes;
    }

    public function getSeconds() {
        return $this->seconds;
    }

    public function add(Duration $d) {
        $this->hours += $d->hours;
        $this->minutes += $d->minutes;
        $this->seconds += $d->seconds;
        while ($this->seconds >= 60) {
            $this->seconds -= 60;
            $this->minutes++;
        }
        while ($this->minutes >= 60) {
            $this->minutes -= 60;
            $this->hours++;
        }
    }

    public function __toString() {
        return implode(':', array($this->hours, $this->minutes, $this->seconds));
    }

}

The public static function fromString() contains a return, and is how the class is called from a script. How does this class use __toString? Why isn't the implode simply included in fromString()?

  • 写回答

3条回答 默认 最新

  • dongxiong5546 2013-05-06 20:48
    关注

    Basically, the one makes Duration objects from strings, the other turns Duration objects into strings.

    This will return an object of class Duration, made from string $string:

    public static function fromString($string)  { ... store input string ... }
    

    If you now want to output the original string, you can't just echo the Duration object itself, because it is not a string but a Duration object.

    If you want to be able to echo a Duration object, you implement the __toString() function:

    public function __toString() { ... build and return output string ... }
    

    And then you can do stuff like this:

    $stringOne = 'Some string';
    $durationObject = Duration::fromString($stringOne);
    echo $durationObject; // Echo-ing the object works because __toString is implemented.
    

    The third line in that code won't work if you don't implement the __toString() method.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 关于大棚监测的pcb板设计
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)