Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CFML inplementation of toString for CFCs

In PHP I could have this situation:

<?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)

I could have sworn there was an equivalent in CFML, eg:

// 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"));

I thought it was new to CF11 or CF2016.

But this dun't work. I know I can do customer serialisers, but that's not a good fit here.

I also know I can effect the same thing in various other ways, but that's not the question here. I am specifically asking about being able to implement a toString method or some such to just be able to specify how to represent an object as a string.

Am I mis-remembering CFML, or am I doing something wrong?

like image 355
Adam Cameron Avatar asked Jul 28 '17 18:07

Adam Cameron


1 Answers

I believe you are thinking of the conversion functions implemented in Lucee...

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

  • _toBoolean()
  • _toDate
  • _toNumeric
  • _toString

To the best of my knowledge, ColdFusion does not have this.

like image 85
Tony Junkes Avatar answered Sep 20 '22 15:09

Tony Junkes