CFML 对 CFC 的 toString 实施

CFML inplementation of toString for CFCs

在PHP我可能遇到这种情况:

<?php
class Person {

    public $firstName;
    public $lastName;

    function __construct($firstName, $lastName)
    {
        $this->firstName = $firstName;
        $this->lastName = $lastName;
    }

    function __toString()
    {
        return "{$this->firstName} {$this->lastName}";
    }
}

echo new Person("Adam", "Cameron"); // Adam Cameron

(runnable demo)

我发誓在 CFML 中有一个等价物,例如:

// Person.cfc
component {

    function init(id, firstName, lastName){
        this.id = id;
        this.firstName = firstName;
        this.lastName = lastName;
    }

    function toString(){
        return "#this.firstName# #this.lastName#";
    }
}

// test.cfm
writeOutput(new Person("Adam", "Cameron"));

我以为是 CF11 或 CF2016 的新手

但这行不通。我知道我可以做客户序列化程序,但这不适合这里。

我也知道我可以通过其他各种方式实现同​​样的效果,但这不是这里的问题。我特别询问是否能够实现 toString 方法或一些类似的方法,以便能够指定如何将对象表示为字符串。

是我记错了 CFML,还是我做错了什么?

我相信你正在考虑Lucee中实现的转换函数...

http://docs.lucee.org/guides/Various/TIPS/TIPS-implicit-conversions.html

  • _toBoolean()
  • _至今
  • _toNumeric
  • _toString

据我所知,ColdFusion 没有这个。