Is there a way to writes Reuseable code(Traits
) like PHP or Javascript in Coldfusion
As this seems to be a great idea of writing Traits and using in Different Classes where they are needed.
I looked at: http://php.net/manual/en/language.oop5.traits.php
And I found the following PHP code to be interesting:
<?php
trait A {
public function smallTalk() {
echo 'a';
}
public function bigTalk() {
echo 'A';
}
}
trait B {
public function smallTalk() {
echo 'b';
}
public function bigTalk() {
echo 'B';
}
}
class Talker {
use A, B {
B::smallTalk insteadof A;
A::bigTalk insteadof B;
}
}
class Aliased_Talker {
use A, B {
B::smallTalk insteadof A;
A::bigTalk insteadof B;
B::bigTalk as talk;
}
}
?>
My take on things like this.
CFML is different from other programming languages in that it has lots of built-in functions. I use objects all the time. The objects I use are application.cfc
, FW/1 Controllers, DI/1 / FW/1 Services, DI/1 / FW/1 Beans, and ORM back objects.
Each and every one of these has a built in set of expectations and usages. None of them are plain objects. It has been a long time since I wrote a plain object. I would much rather use a more feature rich object.
CFML also has member functions on its variables. This makes them similar objects.
Last but not least, If I really had to pull in functionality from multiple locations, I would just create an instance of an object inside of another.
Disclaimer: I currently work on an application with about fifty cfc and hundreds of functions. All of them are needed but some refactoring might be useful.
I could see one or two places where traits might helpful, but I can think of other things to do first.