Blog  |  Contact  
Documentation Customer Center  
What is Railo?
Pricing
Editions
Compatibility
Enhancements
What's new?
Tags
Functions
Language & syntax
Resources
Other improvements
Application Events
Compiler
Magic functions
Tag Libraries
Function libraries
Railo archives
Build In Database
File Paths
FAQ
Installation
Performance
Apps & frameworks

Magic functions

Charming behaviour of components

In an every day situation it might be reasonable to dump the contents of a component. If you dump a component by using the usual <cfdump> tag, all methods of the component will be displayed, but not the values. If you would like to display the contents of a component you have to write an output routine and display each of the elements or properties.
In addition a component cannot be used in the following way:

<cfset myAddress = CreateObject("component", "address.cfc")>
<cfoutput>#myAddress#</cfoutput>

It is clear that the code above throws an error message stating, that the complex objects myAddress cannot be converted into a string. In Railo 2.0 this might be possible by using magic functions. The following magic functions possible:
  • Getters and Setters
  • Implicit conversions
These functionalities can be activated easily by checking the corresponding checkbox in the webadmin. It can vary per webkontext.

Getters & Setters

In order to make life easier for programmers and in order to have more control over the usage of variables of a CFC, Railo 2.0 introduces getters and setters. Getters and setters come into play when a variable of a component is either not accessible (access restriction) or not defined inside a component.

Example:
A address-component stores for example the name of a person. In order to store the name of the person instead of variables named first name and last name only a getter and a setter are defined in the component. With the definition of getters and setters something like a ?virtual properties? emerges. Such components then have according methods to write and read such ?virtual properties?. These methods are called by Railo implicit, when you use the virtual property somewhere in the code. The following code lines should clarify the meaning of virtual properties:

<cfset oAdress.name = "John Doe">

The component might look like follows:

<cfcomponent>
   <cffunction name="setName">
      <cfarguments name="value" required="true" type="string">
      <cfquery name="check4Name">
         ...
      </cfquery>
      <cfif check4Name.recordCount gt 0>
         <cfthrow message="Name already existing">
      <cfelse>
         <cfset this.name = arguments.value>
      </cfif>
   </cffunction>

   <cffunction name="getName" returntype="string">
      <cfreturn this.name>
   </cffunction>
</cfcomponent>

The advantage is obvious. You can do further checking when reading or writing values from/to a ?virtual property?.

Implicit conversions

If you would like to use a component in a certain context (like output as a string) Railo checks, if a method with the corresponding conversion funktionality exists. The following implicit conversion methods are supported by Railo:
  • _toString()
  • _toNumeric()
  • _toBoolean()
  • _toDate()
When a string request is made to a component, Railo searches a method named _toString() and executes it. So you could write the following code:

<cfoutput>#myAddress#</cfoutput>

Where the componente address.cfc looks like follows:

<cfcomponent>
...
   <cffunction name="_toString"> 
      <cfreturn getName() & getAddress() & getComment()>
   </cffunction>

</cfcomponent>
Suggestions?
If you have any suggestions regarding the functionality of Railo, you are welcome to contact us with your proposals and whishes.
Suggestions
Railo 3.0 is free
Next giant step with Railo. By releasing Railo 3.0 the gap to the CFML standard is even closer.
Railo 3.0 Community is available for free without any restrictions in use.
read more
Railo Express (Live version)
Try Railo. Just download and play.
No installation necessary
Railo Express