onMissingMethod Example

CFCs support an onMissingMethod function. By defining an onMissingMethod function in the cfcomponent tag body in the CFC, you can handle calls to methods that are not implemented in the CFC. If an application calls a function that is not defined in the CFC, ColdFusion calls the onMissingMethod function and passes it the requested method's name and arguments.

If you do not define an onMissingMethod function, a call to a method that is not defined in the CFC causes ColdFusion to throw an error that must be handled in the calling code.

The onMissingMethod function is useful for several purposes: To handle errors directly in the component, instead of requiring that each instance of code that calls the component handles them. To create a dynamic proxy, an object that can take arbitrary calls and dynamically determines the correct action.

The onMissingMethod function must have the following format:

<cffunction name="onMissingMethod">
<cfargument name="missingMethodName" type="string">
<cfargument name="missingMethodNameArguments" type="struct">
code to handle call to nonexistent method
</cffunction>

The following example shows usage of onMissingMethod.

This is the code for cfc:

<cffunction name="Sum">
<cfargument name="x" required="true">
<cfargument name="y" required="true">

<cfset total = x + y>
<cfreturn total>
</cffunction>

<cffunction name="OnMissingMethod">

<cfoutput><br><br>*****************************************************<br></cfoutput>
<cfoutput>Hello, Welcome to cf-examples.net...<br>
You are trying to call the method "#arguments.missingMethodName#" with parameters:
#arguments.missingmethodarguments.1# and #arguments.missingmethodarguments.2#
<br> This method does not exist.<br><br>
This is from the onMissingMethod.</cfoutput>
<cfoutput><br><br>*****************************************************<br></cfoutput>

</cffunction>

Following below is the cfm code:

<!--- create an object of the mytest cfc --->
<cfset mytestobj = createobject("component", "mytest")>

<!--- call the method sum with arguments --->
<cfset result = mytestobj.sum("5", "2")>

<!--- output the obtained result --->
<cfoutput>The result value is: #result#</cfoutput>

<!--- Try to call a method which does not exist. The control should go to the onMissingMethod in the cfc. --->
<cfset result = mytestobj.multiply("5", "2")>

You can download the source code using the download button.

Here are a few useful links related to onMissingMethod.

http://livedocs.adobe.com/coldfusion/8/htmldocs/help.html?content=Tags_c_10.html

http://www.bennadel.com/blog/868-Learning-ColdFusion-8-OnMissingMethod-Event-Handler.htm

http://www.coldfusionjedi.com/index.cfm/2008/6/13/Ask-a-Jedi-Problem-using-onMissingMethod-inside-a-CFC

http://www.coldfusionjedi.com/index.cfm/2007/8/5/Warning-about-onMissingMethod

structnew example

The following example shows usage of StructNew.

<cfset employee = StructNew()>

<cfset employee.first = "FirstName">
<cfset employee.last = "LastName">
<cfset employee.value1 = "12345">

<!--- Nested Structure... Have a structure inside a structure. --->
<cfset addr = structnew()>
<cfset addr.nu = "10">
<cfset addr.street = "BG Road">
<cfset addr.city = "Bangalore">

<cfset employee.address = "#addr#">

<cfdump var = #employee#>

You can download the source using the download button.

URLEncodedFormat example

Following is an example to show the usage of the function "URLEncodedFormat".

Description: Generates an URL-encoded string. For example, it replaces spaces with %20, and non-alphanumeric characters with equivalent hexadecimal escape sequences.

Returns: A copy of a string, URL-encoded.

syntax: URLEncodedFormat(string [, charset ])

string: A string or a variable that contains a string.

charset: The character encoding in which the string is encoded. Optional.

The following list includes commonly used values:

* utf-8 * iso-8859-1 * windows-1252 * us-ascii * shift_jis * iso-2022-jp * euc-jp * euc-kr * big5 * euc-cn * utf-16

Here is a simple example:

<cfset enc = URLEncodedFormat("Adobe Cold Fusion 8")>
<br><cfoutput>The URLEncodedFormat of "Adobe Cold Fusion 8" is <br>"#enc#".
</cfoutput>

<br><cfset enc = URLEncodedFormat("JRUN&ColdFusion 8.0.1")>
<cfoutput>The URLEncodedFormat of "JRUN&ColdFusion 8.0.1" is <br>"#enc#".
</cfoutput>

Another example follows:

<h3>URLEncodedFormat Example</h3>
<cfif IsDefined("url.myExample")>
<p>The url variable url.myExample was passed from the previous link ...
its value is:
<br><b>"<cfoutput>#url.myExample#</cfoutput>"</b>
</cfif>
<p>This function returns a URL encoded string.
<cfset s = "My url-encoded string has special characters & other stuff">
<p> <A HREF = "urlencodedformat.cfm?myExample=<cfoutput>#URLEncodedFormat(s)#
</cfoutput>"
>Click me</A>

You can download the code using the download button.

ArrayIsDefined Example

Here is an example to show the usage of ArrayIsDefined function in CF8.

This function determines whether an array element is defined.

Returns YES, if the array element is defined (exists). Returns NO, if the array element is NOT defined.

Function syntax: ArrayIsDefined(array, elementIndex)

<!--- Create a new array --->
<cfset MyArray = ArrayNew(1)>

<!--- Populate the array --->
<cfset MyArray[1] = "First Element">
<cfset MyArray[3] = "Third Element">

<!--- Display the contents of the array --->
<p>
Your array contents are:
<cfdump var="#MyArray#">
</p>

<cfoutput>
<!--- Check if the elements are defined. --->
<p>Does element 1 exist?
#ArrayIsDefined(MyArray, 1)#
<p>Does element 2 exist?
#ArrayIsDefined(MyArray, 2)#
<p>Does element 3 exist?:
#ArrayIsDefined(MyArray, 3)#</p>
</cfoutput>

Note: This function currently throws an error if the array is NOT initialized. So if the array is not yet created, you should surround the ArrayIsDefined code in a block. Also, the ArrayIsDefined function throws an error if you test for an element beyond the length of the array. To prevent this error, you can surround the test in the ArrayLen function.

You can download the cfm code using the download button.

BlogCFC was created by Raymond Camden. This blog is running version 5.9.002. Contact Blog Owner