初用php 物件導向編程 感覺有點彆扭...
因為一直做前端 用js
一般這樣用
function xx(){
this.init.apply(this,arguments);
}
xx.prototype = {
options : {
//幾個預設的屬性
},
init : function(options){
$.extend(this,this.options,options);
//沒有jquery 寫一個extend也很容易
//.........
}
}
new xx(aa) //aa是一個對象 傳一些參數
$.extend(this,this.options,options); 這個就是把 new的時候的參數 和預設的參數 一起給new出來的執行個體 很容易就實現了
今天寫php的時候(同事告訴我要一個一個的賦值)
如下代碼 (合并js css的東西 )
class mergeCompress
{
var $merge_arr;
var $commpress_arr;
var $type;
var $file_dir; //壓縮合并後的目錄
var $file_name; //壓縮合并後的檔案名稱
var $path_jar; // yui jar的路徑
var $is_del = true; //壓縮後的零散檔案是否刪除掉
var $errs = array();
public function __construct($commpress_arr=array(),$merge_arr=array(),$type="js",$file_dir="",$file_name="",$path_jar=""){
$this->type = $type;
$this->commpress_arr = $commpress_arr;
$this->merge_arr = $merge_arr;
$this->file_dir = $file_dir;
$this->file_name = $file_name;
$this->path_jar = $path_jar;
}
public function exec(){
$new_compress_arr = $this->compress();
$this->merge_arr = array_merge($this->merge_arr,$new_compress_arr);
$this->merge();
if($this->is_del){
foreach($new_compress_arr as $i=>$url){
unlink($url);
}
}
}
public function compress(){
$new_compress_arr = array();
$temp = "java -jar {jar_path} --type {type} --charset utf-8 {source} > {compressor_source}";
$temp = str_replace("{jar_path}",$this->path_jar,$temp);
$temp = str_replace("{type}",$this->type,$temp);
$type = $this->type;
$file_dir = $this->file_dir;
foreach($this->commpress_arr as $i => $val){
if(!file_exists($val)){
array_push($this->errs,$val." Does not exist!!!! ");
continue;
}
$cmd = str_replace("{source}",$val,$temp);
$new_name = basename($val);
$new_name = substr($new_name,0,strrpos($new_name,".")).".min.".$type;
$file_new_name = $file_dir."/".$new_name;
$cmd = str_replace("{compressor_source}",$file_new_name,$cmd);
$arr = array();
exec($cmd,$arr,$is);
if($is != 0){
array_push($errs,$val." compress fail!!!! ");
}
array_push($new_compress_arr,$file_new_name);
}
return $new_compress_arr;
}
public function merge(){
$content="";
$file_name = $this->file_dir."/".$this->file_name;
foreach($this->merge_arr as $i=>$url){
if(!file_exists($url)){
array_push($this->errs,$url." Does not exist!!!! ");