初用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<br> {<br> var $merge_arr;<br> var $commpress_arr;<br> var $type;<br> var $file_dir; //压缩合并后的目录<br> var $file_name; //压缩合并后的文件名<br> var $path_jar; // yui jar的路径<br> var $is_del = true; //压缩后的零散文件是否删除掉<br> var $errs = array();<br> public function __construct($commpress_arr=array(),$merge_arr=array(),$type="js",$file_dir="",$file_name="",$path_jar=""){<br> $this->type = $type;<br> $this->commpress_arr = $commpress_arr;<br> $this->merge_arr = $merge_arr;<br> $this->file_dir = $file_dir;<br> $this->file_name = $file_name;<br> $this->path_jar = $path_jar;<br> }<br> public function exec(){<br> $new_compress_arr = $this->compress();<br> <br> $this->merge_arr = array_merge($this->merge_arr,$new_compress_arr);<br> <br> $this->merge();<br> <br> if($this->is_del){<br> foreach($new_compress_arr as $i=>$url){<br> unlink($url);<br> }<br> } <br> }<br> public function compress(){<br> $new_compress_arr = array();<br> $temp = "java -jar {jar_path} --type {type} --charset utf-8 {source} > {compressor_source}";<br> $temp = str_replace("{jar_path}",$this->path_jar,$temp);<br> $temp = str_replace("{type}",$this->type,$temp);<br> $type = $this->type;<br> $file_dir = $this->file_dir;<br> foreach($this->commpress_arr as $i => $val){<br> if(!file_exists($val)){<br> array_push($this->errs,$val." Does not exist!!!! ");<br> continue;<br> }<br> $cmd = str_replace("{source}",$val,$temp);<br> $new_name = basename($val);<br> $new_name = substr($new_name,0,strrpos($new_name,".")).".min.".$type;<br> $file_new_name = $file_dir."/".$new_name;<br> $cmd = str_replace("{compressor_source}",$file_new_name,$cmd);<br> $arr = array();<br> exec($cmd,$arr,$is);<br> if($is != 0){<br> array_push($errs,$val." compress fail!!!! ");<br> }<br> array_push($new_compress_arr,$file_new_name);<br> }<br> return $new_compress_arr;<br> }<br> public function merge(){<br> $content="";<br> $file_name = $this->file_dir."/".$this->file_name;<br> <br> foreach($this->merge_arr as $i=>$url){<br> if(!file_exists($url)){<br> array_push($this->errs,$url." Does not exist!!!! "); <div class="clear"> </div>