Need to use the 'this' scope with OnMissingMethod in the extended class

I hope someone can explain this behavior, as I find it rather aggravating. I have a parent class with an OnMissingMethod implementation to provide implicit getters / setters (old CF8 application)

If I create a child class as foo and call foo.getBar () from an external file, it successfully runs OnMissingMethod, but if inside the class foo I call getBar () - no. The only way to call OnMissingMethod is to use if I use this.getBar () , which I don't like about aesthetic considerations and code inconsistencies.

TL; DR; here is a sample code ... try it yourself.

Foo.cfc

<cfcomponent output="false" extends="Parent"> <cffunction name="init" output="false" returntype="Foo"> <cfreturn this /> </cffunction> <cffunction name="getInternalBar_workie"> <cfreturn this.getBar() /> </cffunction> <cffunction name="getInternalBar_noworkie"> <cfreturn getBar() /> </cffunction> </cfcomponent> 

Parent.cfc

 <cfcomponent output="false"> <cffunction name="OnMissingMethod"> <!--- always return true for this example ---> <cfreturn true /> </cffunction> </cfcomponent> 

foobar.cfm

 <cfset foo = CreateObject( "component", "Foo").init() /> <!--- this works ---> <cfdump var="#foo.getBar()#" /><br/> <!--- this works ---> <cfdump var="#foo.getInternalBar_workie()#" /><br/> <!--- this fails ---> <cfdump var="#foo.getInternalBar_noworkie()#" /> 

Can someone explain why the 'this' scope should be used for OnMissingMethod to work correctly when called from the class itself? Is there a better workaround?

+4
source share
2 answers

Thank you for Google. I did not know the answer, but googled โ€œcoldfusion onmissingmethod this scopeโ€, and the first match explained your situation in one of the comments . I feel bad for reproducing Elliott's work, but answered your question:

[...]

CFCs are just proxied pages. CreateObject () returns a TemplateProxy template that wraps CFPage, which is your actual code.

[...]

when you call a function like "this.getFoo ()" or from out like "myObject.getFoo ()", instead, it calls the method on TemplateProxy to call the method, which in turn calls the function on the proxy page.

OnMissingMethod processing exists in the invoke () function on TemplateProxy, so it only works externally or through the scope.

[...]

+2
source

This shows the difference between public members and private members. The call to getBar() not a shortcut to the public this.getBar() . This is a shortcut to private variables.getBar() , which is not in Parent.cfc . The private function variables.getBar() exists only in the variable Foo.cfc . Since it is a public function, it is also available as an open member of the entire class instance within this . A parent cannot refer to private variables inside an extended (child) object. But a parent can refer to public members.

-1
source

Source: https://habr.com/ru/post/1490583/


All Articles