以往在要使用某個 class 的時候, 都會用 require, require_once 或 include 來讀 class 的檔案, 然後在 new 物件出來。 而 PHP 5 的 autoload 可以簡化這個部份, 使用 autoload 之後就不需要先用 require 來讀取 php, 就可以直接用 new 來建立物件, 而 require 的部份則就交給 autoload 來處理, 稱之為 lazy loading。
最基本的方式就是用 __autoload 函式來處理。
function __autoload($class) {
$class = str_replace('_', '/', $class);
require $class . '.php';
}
而看了幾個為了 PHP 5 寫的 framework 或 package 都採用 spl_autoload_register 來作 autoload 的處理。
Read the rest of this entry »
validation 超難用, 可是他的 ORM 超好用阿!!! 幹…
有了之前設定 CodeIgniter 透過 Gmail 發信的經驗, 這次設定 Kohana 就快很多了 :p
不過 CodeIgniter 是 email library, 而 Kohana 則是 email helper (實際上是使用 swift mailer)。
設定檔 /application/config/email.php
$config['driver'] = 'smtp';
$config['options'] = array(
'hostname' => 'smtp.gmail.com',
'username' => 'your_username',
'password' => 'your_password',
'port' => 465,
'encryption' => 'tls'
);
然後接著就可以用以下的 code 來發信啦!
$to = 'foo@bar.com';
$from = 'bar@foo.com';
$subject = 'email subject';
$message = 'Hello, World!';
email::send($to, $from, $subject, $message);
According to this post in the kohana forum, the find_by_* method of ORM is not avaliable since Kohana 2.2 because the performance issue.
In the previous version, Kohana 2.1.2, I used before. I can use find_by_*() method like this:
$user = new User_Model();
$user->find_by_username('tzangms');
But for now, in Kohana 2.2, we should use where() instead, if we want to find by username, not by id.
$user = new User_Model()
$user->where('username', 'tzangms')->find();
damn, find_by_* is so intuitive.
I encountered a problem with Kohana session library last night, here is the error message below:
Use of undefined constant FILTER_FLAG_NO_PRIV_RANGE – assumed ‘FILTER_FLAG_NO_PRIV_RANGE’
It means that PHP has to enable filter extension. But the document doesn’t mention this at all.
Anyway, I am recompiling PHP with USE=’filter’. Orz