How to Create a Base CFC
- 1). Open the Adobe ColdFusion application and create a new document where you can practice creating a base class without impacting any other code that you've already written.
- 2). Create a private function provides the super constructor functionality that's required to allow sub-classes to work. Constructors are used in code to initiate classes of code when they are needed. A super constructor has the ability to override a regular constructor, whose actions are typically dictated by other portions of your software's code.
<cffunction
name="Init"
access="private"
returntype="void"
output="false"
hint="Base class"> - 3). Define the arguments for the base class to follow by using the code listed below. The base class is the class of code functions that is used as the primary reference point in other parts of the application's code. The base class is called by different portions of code when specific functions need to be executed. The argument in the base class defines how the class should operate and the conditions that need to be met in order for the class to be called by the software.
<cfargument
name="Name"
type="string"
required="true"
hint="This is a base class."
/> - 4). Set up the variables for the instance that subclasses will call when they need to be used and close the first function of the private class. A private class is isolated to the base class in which it belongs. Unlike a public class, a private class cannot be used unless its base (or parent) class is called first.
<cfset VARIABLES.Instance = {
Name = ARGUMENTS.Name
} />
<cfreturn />
</cffunction> - 5). Create the public function that you'll use to house all of the subclasses that run within the base class:
<cffunction
name="GetName"
access="public"
returntype="string"
output="false"
hint="This is a public base class.">
<cfreturn VARIABLES.Instance.Name />
</cffunction>
</cfcomponent> - 6). Save the base CFC code so you can access it in the future and use it as a template in your future coding projects. Paste the base class code into applications that require the use of one and modify the parameters to fit your application's needs.
Source...