Wednesday, July 20, 2011

CentOS install Zend Server CE

Follow the official guide "RPM Installation (RHEL, CentOS, Fedora and OEL)" first.

I install Zend Server CE with "Manually Installing Zend Server" guide. But my Zend Server repository is downloaded from here, enter the Linux tab, download the "Zend Server (DEB/RPM Installer Script)" package. Use the "zend.rpm.repo" repository file.

Then, you can finish the installation with "yum install zend-server-ce-php-5.3"

Link the /usr/bin/php to /usr/local/zend/bin/php
ln -s /usr/local/zend/bin/php /usr/bin/php
Or use "ln -f -s /usr/local/zend/bin/php /usr/bin/php"

Troubleshooting:
1. "Cannot load /usr/local/zend/lib/apache2/libphp5.so into server"
    solution: close SELINUX, /etc/selinux/config >> SELINUX="disabled"

Sunday, June 26, 2011

在 Windows 版的 Zend Server CE 安裝 PEAR

在使用 ZendServer\bin\go-pear.bat 的時候, 會出現 phar...does not have a signature 的錯誤, 這時候只需要修改 go-pear.bat 的內容

original: %PHP_BIN% -d output_buffering=0 -d PEAR\go-pear.phar
change to: %PHP_BIN% -d output_buffering=0 -d phar.require_hash=0 PEAR\go-pear.phar

這樣就可以解決無法安裝 PEAR 的問題

PEAR 安裝完後, 如果有發生 Structures_Graph 的 library 找不到, 去 Pear 下載 Structures_Graph 的 package 回來放到 PEAR 的資料夾, 再執行下面幾個指令:

pear channel-update pear.php.net
pear upgrade pear

最後再 Zend Server CE 內設定 include_path 加入 pear 套件的路徑
如果 go-pear.bat 已經加好了, 就重新啟動 Zend Server 即可使用 pear 套件了

資料來源: http://katsande.com/installing-phpunit-for-php-5-3-on-zendserver

Friday, June 24, 2011

PHP DEBUG 工具 debug_print_backtrace

發現到兩個有用的 debug function
debug_print_backtracedebug_backtrace

debug_print_backtrace() 的功用是將目前程式區塊呼叫的位置開始, 一層一層往上找呼叫的位置, 可以將程式 trace 到最一開始的進入點, 這樣的方式可以有效的找到整個程式是從那一段開始出錯的, Zend_Db 也有實作這樣的功能, 沒想到原來 PHP 裡就已經內建了

debug_print_backtrace() 和 debug_backtrace() 不同的地方在於, debug_backtrace() 是未被格式化的資料, debug_backtrace() 回傳的是一份矩陣的 trace 資料, 而 debug_print_backtrace() 回傳的是已經備格式化過的文字, debug_print_backtrace() 比較方便用來快速檢視, 但若是要寫 LOG 或其他用途, debug_backtrace() 是比較好的選擇

Tuesday, June 07, 2011

camelize and underscore

CakePHP 的 Inflector Class 裡頭找到這段的程式碼, 非常精簡地寫法!
/**
 * Returns given $lower_case_and_underscored_word as a camelCased word.
 *
 * @param string $lower_case_and_underscored_word Word to camelize
 * @return string Camelized word. likeThis.
 * @access public
 * @static
 */
function camelize($lowerCaseAndUnderscoredWord) {
  $replace = str_replace(" ", "", ucwords(str_replace("_", " ", $lowerCaseAndUnderscoredWord)));
  return $replace;
}
/**
 * Returns an underscore-syntaxed ($like_this_dear_reader) version of the $camel_cased_word.
 *
 * @param string $camel_cased_word Camel-cased word to be "underscorized"
 * @return string Underscore-syntaxed version of the $camel_cased_word
 * @access public
 * @static
 */
function underscore($camelCasedWord) {
  $replace = strtolower(preg_replace('/(?<=\\w)([A-Z])/', '_\\1', $camelCasedWord));
  return $replace;
}

Friday, May 20, 2011

資料快取的概念


class test
{
  protected $_tmp = array();
  
  public function foo()
  {
    $id = '123';
    $tmp = $this->get_tmp($id);
    echo '
';
    var_dump($tmp);
    echo '
'; } public function bar() { $id = '223'; $tmp = $this->get_tmp($id); echo '
';
    var_dump($tmp);
    echo '
'; } public function get_tmp($id) { // 已經有資料時直接回傳 if (count($this->_tmp) > 0) { if (isset($this->_tmp[$id])) { return $this->_tmp[$id]; } return FALSE; } // 取得資料 $tmp = array( '123' => array('id' => '123', 'name' => 'xdy'), '223' => array('id' => '223', 'name' => 'xdasdfsafdy'), '323' => array('id' => '323', 'name' => 'xsdfsdfdy'), '423' => array('id' => '423', 'name' => 'xsadasdasdffdy'), ); // 作快取 $this->_tmp = $tmp; // 檢查是否有需要的資料 if (isset($this->_tmp[$id])) { return $this->_tmp[$id]; } return FALSE; } } $test = new test(); $test->foo(); // 抓取的資料會做第一次的快取 $test->bar(); // 這次就會直接從快取抓資料 $tmp = $this->get_tmp('323'); // 這次也是直接抓快取資料