顯示具有 PHP 標籤的文章。 顯示所有文章
顯示具有 PHP 標籤的文章。 顯示所有文章

2013年11月23日

[轉載] PHP 類別繼承的建構子與解構子

PHP 類別的繼承及建解構子的幾個注意點

1. php 的建構子會在類別實體化後執行,寫法有兩種,一個是用 __construct() 保留字,另一個是用和類別同名的函式

class BASE{
   function __construct(){
      print "BASE constructor";
   }
}

或是

class BASE{
   function BASE(){
      print "constructor function same name with class";
   }
}

若同時有兩個建構子,則以  __construct() 為優先,同類別名的函數將不會被執行。

我個人建議使用 __construct(),因為類別可能因為改名或複製,常會忘記把建構子也改名。

class BASE{
   function BASE(){
      print "constructor function same name with class";
   }
   function __construct(){
      print "BASE constructed";
   }
}

執行結果:
BASE constructor

2. php 的解構子會在1程式執行到結尾 2程式exit 3物件被unset 後被執行,寫法用 __desctruct() 保留字

class BASE{
   function __construct(){
      print "BASE constructedn";
   }
   function __destruct(){
      print "BASE destructedn";
   }
}

執行:

$obj =new BASE();
unset($obj);   <== 物件被 unset
print "ENDn";
exit;

結果
BASE constructed
BASE destructed
END

3. 建構和解構函數只不過是一個函數,預設是 public,都可以被叫用。

4. 子類別繼承父類別後,父類別的建解構子都不會被執行

曾經我在這被卡了很久,因為別的語言(java, c#)會去叫用父類別的建解樣子後再處理子類別的。


class BASE{
   function BASE(){
      print "constructor function same name with class";
   }
   function __destruct(){
      print "BASE destructed";
   }
}

class CHILD extends BASE{
   function __construct(){
     print "CHILD constructed";
   }
   function __destruct(){
     print "CHILD destructed";
   }
}

執行
$obj =new CHILD();
exit;

結果
CHILD constructed
CHILD destructed

5. 子類別要執行父類別的建解構子,就要特別叫用,使用 parent:: 保留字

class BASE{
   function BASE(){
      print "constructor function same name with class";
   }
   function __destruct(){
      print "BASE destructed";
   }
}

class CHILD extends BASE{
   function __construct(){
     parent::__construct();
     print "CHILD constructed";
   }
   function __destruct(){
     parent::__destruct();
     print "CHILD destructed";
   }
}

執行
$obj =new CHILD();
exit;

結果
constructor function same name with class
CHILD constructed
BASE destructed
CHILD destructed


資料來源 : http://note.tc.edu.tw/684.html

2013年11月21日

PHP 動態變數名稱 Dynamic variable names in PHP

${'a' . 'b'} = 'hello there';
echo $ab; // hello there


用大括號包起來,前面加個$就可以了。

這個方法適用在變數,若要動態取常數,請參閱
http://subocheng.blogspot.tw/2013/11/php-dynamic-constant-name.html

PHP 動態常數名稱 Dynamic constant name

//定義Constant
define( CONSTANT_1 , 'value1' ) ;
define( CONSTANT_2 , 'value2' ) ;
define( CONSTANT_3 , 'value3' ) ;


//動態調用
echo constant( 'CONSTANT_' . 1);
echo constant( 'CONSTANT_' . 2);
echo constant( 'CONSTANT_' . 3);


這個方法適用在常數,若要動態取變數,請參閱
http://subocheng.blogspot.tw/2013/11/php-dynamic-variable-names-in-php.html

2013年11月15日

另外一種利用PHP寫排程的方法

這是一種非正規的方法。
我的目的是要系統定時去執行一段PHP的程式碼,幫我同步一些資料到Memcache內,方便使用者可以快速的讀取資料。
過程中我需要用到Web Server的一些Memcache特性,因此我需要定時去執行一個URL。

1. 首先用PHP寫一個程式,並放到Web Server下。
     例如:http://127.0.0.1/testSync/Sync.php

2. 另外寫一個AutoRun.php,放到D:\test\AutoRun.php
<?php
set_time_limit(1200);    //設定該Function最多只能執行20min

//Do Sync
$url = "http://127.0.0.1/testSync/Sync.php";
$ch = curl_init ($url);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, true);
$returndata = curl_exec ($ch);
curl_close($ch);

?>

3. 寫一個bat檔案,裡面的內容如下:
"C:\Program Files (x86)\PHP\php.exe" "D:\test\AutoRunSync.php"

前面這個php.exe的位置為你電腦安裝的php路徑(IIS CGI指定的路徑)

4. 最後將步驟3所產生的bat,加到Windows 排程去,設定讓他自動定時執行。

Finish.....

2013年9月12日

PHP 單引號 和 雙引號 的差別

PHP 會解譯「雙引號」字串內的變數,而「單引號」則視為純字串出,PHP 不會再處理單引號內的內容。

PHP Parse HTTP Header

function http_parse_headers( $header )
{
    $retVal = array();
    $fields = explode("\r\n", preg_replace('/\x0D\x0A[\x09\x20]+/', ' ', $header));
    foreach( $fields as $field ) {
        if( preg_match('/([^:]+): (.+)/m', $field, $match) ) {
            $match[1] = preg_replace('/(?<=^|[\x09\x20\x2D])./e', '("\0")', strtolower(trim($match[1])));
            if( isset($retVal[$match[1]]) ) {
                $retVal[$match[1]] = array($retVal[$match[1]], $match[2]);
            } else {
                $retVal[$match[1]] = trim($match[2]);
            }
        }
    }
    return $retVal;
}

最後他會回傳一個Array

2013年9月1日

Javascript 加密,PHP解密 AES加密方式

JavaScript 加密:

<script src="http://crypto-js.googlecode.com/svn/tags/3.0.2/build/rollups/aes.js"></script>
<script src="http://crypto-js.googlecode.com/svn/tags/3.0.2/build/components/pad-zeropadding.js"></script>

function EncryptData(data)
{
var key = CryptoJS.enc.Latin1.parse("1234567812345678");
var iv  = CryptoJS.enc.Latin1.parse("1234567812345678");
var encrypted = CryptoJS.AES.encrypt(data, key, { iv: iv,mode:CryptoJS.mode.CBC,padding:CryptoJS.pad.ZeroPadding});
return encrypted;
}


PHP解密
$key = "1234567812345678";
$iv = "1234567812345678";
$DecryptData = rtrim(mcrypt_decrypt(MCRYPT_RIJNDAEL_128, $key, base64_decode($EncryptedData), MCRYPT_MODE_CBC, $iv),"\0");


注意一點,要把解密出來的後面\0來掉。
所以加了rtrim(XXX,"\0");
要不然Javascript比對會錯誤。


下面這一個我寫了一個SampleCode,但Key和iv的地方,需要做修改
Sample Code Download :

http://sdrv.ms/1aazyLW