当前位置:Gxlcms > PHP教程 > 初用php面向对象编程感觉有些别扭.

初用php面向对象编程感觉有些别扭.

时间:2021-07-01 10:21:17 帮助过:23人阅读

初用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!!!! ");

人气教程排行