Skip to main content
Inspiring
January 7, 2022
Answered

CF2016 vs CF2021 Component Behavior

  • January 7, 2022
  • 11 replies
  • 600 views

Support for CF2016 is coming to a close and as such, I'm looking into upgrading our application to CF2021. Running our application in CF2021, I've ran into multiple issues - some easily fixed, some not so much... the most concerning is the following: 

 

If you have two components, componentA and componentB and both components have a function named the same ( e.g. getID() ) and inject componentB into componentA, if componentB calls the getID() function, it actually calls the getID() function in componentA rather than calling the getID() function local to componentB.
 
Is this a new behavior in cf2021?

 

 

    This topic has been closed for replies.
    Correct answer --jojo--

    @--jojo-- , you could refine the test code as follows:

      public any function doSomething(){
         if( getID() == 0 ){
              WriteLog(file="getIDTest", text="This line of log confirms that getID() is 0. Hence componentA.getID() has been called."); 
         } else if ( getID() == 1 ) {
              WriteLog(file="getIDTest", text="This line of log confirms that getID() is 1. Hence componentB.getID() has been called."); 
         }
      }
    

     

     

     

     


    Thanks, everybody!  It was environmental, after restarting the server altogether, it started working as it should. Onto the next issue now. 

     

    @Charlie Arehart looks like I misunderstood the support matrix document - I was looking at the extended support date for 2016 (2/17/2022). Core support ended last year (yikes!)

    11 replies

    BKBK
    Community Expert
    Community Expert
    January 7, 2022

    The key word in your description is "inject". What do you mean by "inject componentB into componentA"?

     

    A code example would be great. Especially, one that fails on CF2021 but works on CF2016.

    --jojo--Author
    Inspiring
    January 7, 2022
    componentA.cfc
    component {
      property name="componentB";
    
      public any function init(){
        // do your init functions
      } 
    
      public numeric function getID(){
         return 0;
      }
    
      public any function getMeSomething(){
         return variables.componentB.doSomething();
      }
    }
    
    componentB.cfc
    component {
    
      public any function init(){
        // do your init functions
      } 
    
      public numeric function getID(){
         return 0;
      }
    
      public any function doSomething(){
         if( getID() ){
            //do something
         }
         return;
      }
    }
    
    index.cfm
    objComp = new componentA();
    objComp.setComponentB( new componentB() );
    
    test = objComp.getMeSomething();
    BKBK
    Community Expert
    Community Expert
    January 7, 2022

    Thanks for the code example. I put the following 3 files in the same directory under the web root. 

     

    When I run index.cfm, a log file gets created, containing output "This line of log confirms that getID() is 1. Hence componentB.getID() has been called.". Which is what I expect. I am on CF2021.

     

     

    /* ComponentA.cfc */
    component accessors="true" {
    	
      property name="componentB";
    
      public any function init(){
        // do your init functions
      } 
    
      public numeric function getID(){
         return 0;
      }
    
      public any function getMeSomething(){
         return variables.componentB.doSomething();
      }
    }

     

     

     

    /* ComponentB.cfc */
    component accessors="true" {
    
      public any function init(){
        // do your init functions
      } 
    
      public numeric function getID(){
         return 1;
      }
    
      public any function doSomething(){
         if( getID() != 0 ){
              WriteLog(file="getIDTest", text="This line of log confirms that getID() is 1. Hence componentB.getID() has been called."); 
         }
         return;
      }
    }
    

     

     

     

    /* index.cfm */
    <cfscript>
    objComp = new componentA();
    objComp.setComponentB( new componentB() );
    
    test = objComp.getMeSomething();
    
    writeoutput("Test done.")	
    </cfscript>