用于CFC的toString的CFML补充

Ada*_*ron 7 coldfusion cfml

在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
Run Code Online (Sandbox Code Playgroud)

(可运行的演示)

我可以发誓在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"));
Run Code Online (Sandbox Code Playgroud)

我认为这对CF11或CF2016来说是新的.

但这不行.我知道我可以做客户序列化服务器,但这不适合这里.

我也知道我可以通过其他各种方式实现相同的目标,但这不是问题.我特别要求能够实现一个toString方法或类似的方法,以便能够指定如何将对象表示为字符串.

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

Ton*_*kes 7

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

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

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

据我所知,ColdFusion没有这个.