Als ich vorher Euer Klasse Tutorial unter https://docs.lucee.org/guides/cookbooks/StaticScope.html durchgespielt habe hat das irgendwie nie so funktioniert wie es sollte. Habs mir dann mal genauer angeschaut…das sind zwei kleine aber echt fiese Fehler im Code weil man zuerst irgendwie an ganz andere Dinge denkt:
// Example0.cfc Component { private function init(){ dump("create an instance of" & listlast(getCurrentTemplatePath(),'\/')); } public function hey(){ dump("salve !"); } }
statt private function init() müsste es public function init() sein sonst kriegt man den mehrmaligen Dump ja nicht.
// Example2.cfc Component { public static function getInstance(required string lastname, required string firstname) { var id = hash(lastname&":"&firstname,"quick");
if(isNull(static.instance[id])){ static.instance[id] = new Example2(lastname,firstname); } return static.instance(id); } private function init(required string lastname, required string firstname) { dump("create an instance of" & listlast(getCurrentTemplatePath(),'\/')&" for "&lastname&" "&firstname);
variables.lastname = arguments.lastname; variables.firstname = arguments.firstname; } public function hey(){ dump("salve #variables.firstname#!"); } }
Beim Returnwert von getInstance() brauchts eckige Klammern um den Key der Struct korrekt zurückzugeben.
When I played through your class tutorial at Static scope tutorial, it somehow never worked as it should.
A closer look … these are two small but really nasty mistakes in the code because you think of something completely different at first:
// Example0.cfc
Component {
private function init () {
dump ("create an instance of" & listlast (getCurrentTemplatePath (), '\ /'));
}
public function hey () {
dump ("salve!");
}
}
instead of private function init () it would have to be public function init () otherwise you will not get the multiple dump.
// Example2.cfc
Component {
public static function getInstance (required string lastname, required string firstname) {
var id = hash (lastname & ":" & firstname, "quick");
if (isNull (static.instance [id])) {
static.instance [id] = new Example2 (lastname, firstname);
}
return static.instance (id);
}
private function init (required string lastname, required string firstname) {
dump ("create an instance of" & listlast (getCurrentTemplatePath (), '\ /') & "for" & lastname & "" & firstname);
variables.lastname = arguments.lastname; variables.firstname = arguments.firstname;
}
public function hey () {
dump ("salve # variables.firstname #!");
}
}
The return value of getInstance () requires square brackets to return the key of the struct correctly. return static.instance (id); -> return static.instance [id]