初用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!!!! ");
continue;
}
$content=$content.file_get_contents($url)."\n\r";
}
$hd = fopen($file_name,"w+");
$is = fwrite($hd,$content);
fclose($hd);
if(!is){
array_push($this->errs,$file_name." merge fail!!!!! ");
}
}
public function printErr(){
$errs = $this->errs;
$len = count($errs);
if($len == 0){
echo "no err";
}else{
foreach($errs as $i=>$err){
echo $err."<br><br><br>";
}
}
}
}
$commpress_arr = array(
"f2e/assist/js/jquery/merger.js",
"f2e/assist/js/business/broker.js",
"f2e/assist/js/business/http.js",
"f2e/assist/js/components/components.js",
"f2e/assist/js/models/model.js",
"f2e/assist/js/models/vo.js",
"f2e/assist/js/controllers/moduleController.js",
"f2e\assist\js\projectInit.js",
);
$type ="js";
$file_dir = "f2e/assist/js/compression";
$file_name = "merge_all.js";
$path_jar = "D:\yuicompressor\yuicompressor.jar";
$x = new mergeCompress($commpress_arr,array(),$type,$file_dir,$file_name,$path_jar);
$x->exec();
foreach($this as $name=>$v) {
if(isset($$name)) $this->$name = $$name;
}
class a
{
function __construct($arr)
{
foreach ($arr as $k=>$v)
{
$this->$k = $v;
}
}
public function exec()
{
echo $this->abc;
}
}
$arr=array('abc'=>1, 'cba'=>2);
$c = new a($arr);
$c->exec();