매우 간단한 클래스 나 모델을 작성하고 cfscript에서 사용하는 간단한 예제를 찾을 수 없습니다. 여기 cfscript에서 클래스/객체를 만들고 사용하는 방법은 무엇입니까?
는 PHP, 내가 원하는 무엇인가 : oocaller.php : 이<?php
include 'oo-model.php';
$dog = new animal('dog');
echo $dog->says();
OO-모델 :
<?php
class animal{
public $type;
function __construct($type){
$this->type =$type;
}
function says(){
if($this->type == 'dog')
return 'woof!';
if($this->type == 'cat')
return 'meow!';
return 'I dont know what to say!';
}
}
확인이 작동 ...
oocaller.cfm :
<cfscript>
//include 'oomodel.cfc'; //dont need, autoincluded
//obj = new admin.d.oomodel(); //for path, use dots not slashes...
//obj = CreateObject("component", "oomodel");
obj = CreateObject("component", "/admin/d/oomodel");//either works
obj.out();
writeoutput('<HR>');
writedump(obj.other);
writeoutput('<HR>');
writedump(obj);
</cfscript>
로 : oomodel.cfc :
component {
thevar = 'thevrrrrr';
this.other = 'otherrrr';
public function out(){
writeoutput(variables.thevar & "<BR>");
writeoutput(thevar & "<BR>");
writeoutput(this.other & "<BR>");
}
}
이 구성 요소는 자체에 포함 된 "클래스"해야합니다 : 당신은 그냥 ColdFusion과 함께 시작처럼
내가 제안 할 수 있습니다, 보이는 중괄호. 그런 다음 구성 요소를 별도의 파일 (cfm 템플릿 또는 다른 구성 요소)에서 객체로 인스턴스화합니다. 또한 구성 요소 블록 외부의 코드를 별도의 파일로 이동하면 태그를 제거 할 수 있습니다. 구성 요소는 스크립트로 전체적으로 작성 될 수 있습니다. –