SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Addict Bloodfest's Avatar
    Join Date
    Feb 2006
    Posts
    229
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Component Variables

    I would like to use a variable that i declare and assign outside of a component, inside a component..

    How can I do this?

    Right now I'm having to set an application variable to use with in the component tag..

    {EDIT}
    What I'm trying to do...

    Use a component to set global variables like:

    ROOT_PATH
    IMAGES_PATH
    INSTALL_PATH

    I want to invoke this component in my Application.cfm...

    Then I want to use these variables in other components...

  2. #2
    SitePoint Member
    Join Date
    Jan 2005
    Location
    Vancouver, WA
    Posts
    20
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If you want to have direct access to the variables, they will have to be in a "shared" scope like the application scope (or session or request scope, etc.) The only other option is to create something like use "getter" and "setter" methods of your component to access the variables:

    In Application.cfm (or .cfc):

    <cfset component.setRootPath("/path/to/root") />

    Then in your other components:

    <cfset rootPath = component.getRootPath() />

    Of course, this requires that your component is in a shared scope or that you pass a reference to your component when you instantiate the second component.

  3. #3
    SitePoint Addict Bloodfest's Avatar
    Join Date
    Feb 2006
    Posts
    229
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    could somethings like this work?

    component setting global vars
    <cfcomponent>

    set var1
    set var2

    </cfcomponent>

    <cfcomponent extends="(the global component)">

    use the global vars... ie.. #root=path#

    </cfcomponent>

    i'm thinking how Classes extend other Classes..

  4. #4
    SitePoint Evangelist
    Join Date
    Mar 2007
    Posts
    584
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    could somethings like this work?
    Yes, you could extend a base component. The base component's variables would then be available to any cfcs that extended it. One difference is the values would be hard coded into the component. Though as RichardDavies mentioned you could also add a public setter() method to override the values.

    Code:
    <!--- display the root path --->
    <cfset sub = createObject("component", "SubComponent").init() />
    <cfdump var="#sub.getRootPath()#" />
    
    
    <!--- BaseComponent.cfc --->
    <cfcomponent output="false">
    	<cfset variables.instance = structNew()>
    	<cffunction name="init" returntype="BaseComponent">
    		<cfset setRootPath("Your ROOT_PATH VALUE") />
    		<cfreturn this />
    	</cffunction>
    	
    	<cffunction name="getRootPath" access="public" output="false" returntype="string">
    		<cfreturn variables.instance.rootPath />
    	</cffunction>
    
    	<cffunction name="setRootPath" access="private" output="false" returntype="void">
    		<cfargument name="rootPath" type="string" required="true" />
    		<cfset variables.instance.rootPath =  arguments.rootPath />
    	</cffunction>
    </cfcomponent>
    
    <!---SubComponent.cfc --->
    <cfcomponent extends="BaseComponent" output="false">
    	<cfset variables.instance = structNew() />
    	<cffunction name="init" returntype="SubComponent">
    		<cfset super.init() />
    		<cfreturn this />
    	</cffunction>
    </cfcomponent>

  5. #5
    SitePoint Wizard creole's Avatar
    Join Date
    Oct 2000
    Location
    Nashvegas Baby!
    Posts
    7,845
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It's not good to reference shared scopes (application, session, server) from within a CFC. It's been proven that potential memory leaks could occur. Your best bet is to have an explicit init method, then pass in the values which need to be used by any other method in the CFC. You'd do that like this:

    PHP Code:
    <cfcomponent displayname="myCFC" output="false">

        <
    cfset VARIABLES.dsn "">
        <
    cfset VARIABLES.userid "">

        <
    cffunction name="Init" output="false" access="public" returntype="myCFC">
            <
    cfargument name="dsn" required="true" type="string">
            <
    cfargument name="userid" required="true" type="string">
            <
    cfset VARIABLES.dsn ARGUMENTS.dsn>
            <
    cfset VARIABLES.userid ARGUMENTS.userid>
            <
    cfreturn THIS>
        </
    cffunction>

        <
    cffunction name="someMethod" output="false" access="public" returntype="query">
            <
    cfset var ''>
            <
    cfquery name="q" datasource="#VARIABLES.dsn#">
                
    SELECT someColumn
                FROM someTable
                WHERE userid 
    = <cfqueryparam value="#VARIABLES.userid#" cfsqltype="cf_sql_integer">
            </
    cfquery>
            <
    cfreturn q>
        </
    cffunction>

    </
    cfcomponent
    Then you'd instantiate the CFC like so:
    <cfset VARIABLES.myCFC = Createobject('component','myCFC').init('myDSN',123456)>

    Essentially, the VARIABLES scope inside the CFC is available to every method within the CFC. In fact you can accidentally create a variable in the variables scope if you don't explicitly create it in the var (local) scope. One of the main benefit to this is that your code is more self-documenting. Rather than just referencing any value in any scope, you're telling the code where the values are coming from and you can easily change it in once place.
    Adobe Certified Coldfusion MX 7 Developer
    Adobe Certified Advanced Coldfusion MX Developer
    My Blog (new) | My Family | My Freelance | My Recipes


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •