Anooj
Anooj

Reputation: 286

what is the life cycle of a ColdFusion CFC instance creation?

I would like to know how a cfc is instantiated in coldfusion under the hood !. I know you create an instance of the component and you will get a reference to the newly created instance and you can use it to call the public methods inside them.

But what exactly is happening when i write th code

<cfscript>
 person = createObject('component','human') // what happen here!!!!
 person.speak();
</cfscript>

Made some correction to my statement here!!!. the reason why i ask this question is because i have a instance stored in the application scope and the instance is used as below

application.person.speak();

now under very high load. i found that the memory is not release for the person obj and at some point it reached up to 200mb.. strange! . So made the correction as it says in best practices

request.person = duplicate(application.person);

now there is another direct way to do this is

request.person = createObject('component','human');

difference, the first one creates the object and keep it in share scope, do a deep copy to request everytime a request is made(here the instance is created only once). Second one does the instance creation every time the request is made. there is a performance difference between them for sure because in the second method the instance is created every time. I would like to know what exactly is the architecture behind creating an instance that makes it less better that the former!!

Just curies to know !

Upvotes: 1

Views: 1186

Answers (3)

Stefano D
Stefano D

Reputation: 958

Coldfusion compiles into Java, and when you call the "createObject" function you are creating an instance of that class. Here are some links that might help explain a little more:

http://www.bennadel.com/blog/325-ColdFusion-CreateObject-vs-Java-NewInstance-.htm

http://blog.objectorientedcoldfusion.org/2009/07/16/coldfusion-9-object-creation-performance/

Upvotes: 3

Henry
Henry

Reputation: 32905

What happen there? Nothing special really.

For CFC's, init() is just a method that is used to initialize the instance. It is not even required for CFC's if you don't need a constructor. However, it is often included by convention adopted by the community, originated from ColdFusion's choice of using init() for invoking Java constructor.*

function init()
{
  // init vars in Variable scope if needed...

  return this;
}

In CF9, when you use the new operator, it'll call init() for you if it is available, and it won't throw any error if your CFC has no init() method defined.

*When it comes to invoking Java methods, init() is translated to invoke the correct constructor of the Java class. If you just want to call a static method of the class, call the method directly without init().

Upvotes: 0

crosenblum
crosenblum

Reputation: 1927

From what I understand, it is running the init method/function inside that cfc, then running the speak method/function.

Upvotes: 0

Related Questions