質問

What's the difference between these two blocks of code, when called right after a cffunction tag:

<cfparam name="bork_bork_bork" default="false">

<cfargument name="bork_bork_bork" required="false" default="false">
役に立ちましたか?

解決

<cfparam>, when used with the default attribute, will ensure that a variable exists. Because there is no scope specified, bork_bork_bork is being put in to the Variables scope.

<cfargument> is used to pass an arguments to a function. These values are stored in the Arguments scope. You would access the value using arguments.bork_bork_bork.

Note that arguments.bork_bork_bork and bork_bork_bork are not the same. The scope of arguments is only within the function, the other is being stored in the Variables scope and will be valid anywhere on the page (though I would not recommend coding it that way.)

他のヒント

cfparam has nothing to do with functions. I can see that this is confusing, given that param/argument are interchangeable words in most languages. Keep in mind that user-defined functions weren't added to CF until version 5, so there was no conflict in using cfparam as a way to initialize variables. Moreover, the cfparam tag probably drew its name from the now obsolete function, ParameterExists() (or vice versa - by the time I got into CF, at version 4.0 (1999), that function was already deprecated, so I missed the history behind it)

cfparam is a way to set a default for any variable if the variable doesn't already exist. It's a shortcut to do the following:

<cfif NOT isDefined('bork_bork_bork')>
    <cfset bork_bork_bork = 'myDefaultSetting'>
</cfif>

cfargument is only useable after a opening tag to define an argument being passed to a CFC function or a user-defined function.

From what I recall, nothing can exist between the cffunction tag and cfargument tag, so they must appear right after the cffunction tag.

From within the function you'll access cfargument via the arguments scope {arguments.bork_bork_bork} or via an array {arguments1}

cfparam will just ensure the variable is available on the request is should not be used instead of cfargument. For further reading check out:

cfargument

Using the Arguments scope as an array

Defining functions by using the cffunction tag

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top