初用php 物件導向編程 感覺有些彆扭.

來源:互聯網
上載者:User
初用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!!!! ");
  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.