佳音的博客

2007/05/16

理解PHP中的MVC之模型

Filed under: Uncategorized — 佳音 @ 10:42 上午

Implementing MVC in PHP: The Model

by Joe Stump
03/02/2006

This article series (starting with Understanding MVC in PHP and continued in Implementing MVC in PHP: The Controller and Implementing MVC in PHP: The View) demonstrates how to build an MVC web framework using PHP 5. This article explains the Model part of Model-View-Controller design.

Click here to find out more!

The Model is where the majority of the application’s logic sits. It is where you run queries against the database and perform calculations on input. A good example of what a Model would look like is a simple login script. The login script gets user input from a form, validates it against the database, and then logs in the user.

The first application using the newly created framework will be the users module. I will be creating three Models:

  • login.php validates input from a form against the database
  • logout.php logs a user out and destroys the associated session
  • whoami.php displays simple user information, similar to the Unix program of the same name

Because I am introducing the idea of sessions and users into the framework, I will need to create a few more foundation classes as well as a table in a database. Before I go over the code of login.php, I’d like to walk through these classes.

FR_Session

FR_Session is a wrapper for the built-in PHP sessions. The code isn’t very involved and provides only basic support for starting, destroying, and writing sessions.


	

Implementing MVC in PHP: The View

Filed under: Uncategorized — 佳音 @ 10:39 上午

Implementing MVC in PHP: The View

by Joe Stump
01/26/2006

This is the third article in a series about understanding MVC in PHP. The first article explained basics of MVC programming in PHP. The second article showed the implementation of the Controller in MVC. The final article is Implementing MVC in PHP: The Model.

Click here to find out more!

The presentation layer, as I call it, is the View, in common MVC terms. Its sole responsibility is to display information. It could care less about authenticating users, what the data is or, for the most part, where it came from. The only thing it has to worry about is how to render it and where to send it once rendered.

By default, the framework uses Smarty to render the framework. I’m not here to argue semantics, but your presentation layer should consist of a template engine of some sort and a few supporting presentation layers.

The idea is that, after the Model runs, the framework hands it off to a child of the FR_Presenter_common class. Actually, the framework uses the FR_Presenter::factory() to create the presenter. Each presenter should have a display() method that does the actual rendering. When the factory creates the presenter, it passes the presenter the instance of the model class. The presenter then gets the model’s data using its getData() method. From there, the presenter is free to present that data however it sees fit.

FR_Presenter_smarty

The way I’ve created my Smarty presenter is a hybrid of two templates. I create a Smarty template, and the outer page template includes the model’s template. The model class’s $pageTemplateFile can request a particular outer template. If it does not, the default is tpl/default/templates/page.tpl. The page.tpl template then uses the {$modulePath} and {$tplFile} directives to include the model’s template. All model templates should reside in modules/example/tpl/.

After assigning the variables, the controller runs Smarty’s display function to render the templates. With little modification, you could wrap these calls with Smarty’s built-in caching as well. By using Smarty, you could enable an output modifier to output gzipped code instead of plain HTML.


	

2007/05/13

基于既定词表的自适应汉语分词技术研究 [转]

Filed under: Uncategorized — 佳音 @ 5:10 下午

基于既定词表的自适应汉语分词技术研究

黄水清

2007/05/11

理解PHP中的MVC编程之控制

Filed under: Uncategorized — 佳音 @ 8:01 下午

简单来讲,控制器的作用就是接受请求。它使用获取的方法,在这里是通过URI,载入一个功能模块来刷新或者提交一个表述层。控制器将使用$_GET自动全局变量来判断载入哪一个模块。

  一个请求的例子,看起来像这样:

  http://example.com/index.php?module=login

  这看起来很简单,但是在实现的过程中却不是。这里是几个控制器能识别的argument部分:

  module定义了使用哪一个模块,如users模块
  class定义了使用哪一个功能类,如你想让用户login还是logout
  event定义了使用哪一个具体事件

  这样一个更复杂的例子可以解释上面的各个argument最终组成的请求URL:

  http://example.com/index.php?module=users&class=login

  这段请求告诉控制器应该载入users模块,然后是login类,最后因为没有定义具体事件,所以运行login::__default()默认事件。

  以下是具体代码部分:

<?php
 /**
  * index.php
  *
  * @author Joe Stump <joe@joestump.net>
  * @copyright Joe Stump <joe@joestump.net>
  * @license http://www.opensource.org/licenses/gpl-license.php
  * @package Framework
 */

 require_once(‘config.php’);

 // {{{ __autoload($class)
 /**
  * __autoload
  *
  * Autoload is ran by PHP when it can’t find a class it is trying to load.
  * By naming our classes intelligently we should be able to load most classes
  * dynamically.
  *
  * @author Joe Stump <joe@joestump.net>
  * @param string $class Class name we’re trying to load
  * @return void
  * @package Framework
 */

 function __autoload($class)
 {
  $file = str_replace(‘_’,'/’,substr($class,2)).’.php’;
  require_once(FR_BASE_PATH.’/includes/’.$file);
 }
 // }}}

 if (isset($_GET['module'])) {
  $module = $_GET['module'];
  if (isset($_GET['event'])) {
   $event = $_GET['event'];
  } else {
   $event = ‘__default’;
  }

 if (isset($_GET['class'])) {
  $class = $_GET['class'];
 } else {
  $class = $module;
 }

 $classFile = FR_BASE_PATH.’/modules/’.$module.’/’.$class.’.php’;
 if (file_exists($classFile)) {
  require_once($classFile);
  if (class_exists($class)) {
   try {
    $instance = new $class();
    if (!FR_Module::isValid($instance)) {
     die(“Requested module is not a valid framework module!”);
    }

    $instance->moduleName = $module;
    if ($instance->authenticate()) {
     try {
      $result = $instance->$event();
      if (!PEAR::isError($result)) {
       $presenter = FR_Presenter::factory($instance->presenter,$instance);

      if (!PEAR::isError($presenter)) {
       $presenter->display();
      } else {
       die($presenter->getMessage());
      }
     }
    } catch (Exception $error) {
     die($error->getMessage());
    }
   } else {
    die(“You do not have access to the requested page!”);
   }
  } catch (Exception $error) {
   die($error->getMessage());
  }
 } else {
  die(“An valid module for your request was not found”);
 }
 } else {
  die(“Could not find: $classFile”);
 }
 } else {
  die(“A valid module was not specified”);
}

?>

  接下来是以上代码具体的注释:

  载入“config.php”

  定义__autoload()函数。这是PHP5里面的一个新函数,方便动态地载入各个类。

  如果一个argument被定义,那么载入相关的模块、类和具体事件

  接下来就是一些判断以及错误的具体操作

  最后一切无误后就载入表述层

  【友好URL】

  如果你觉得上面例子讲到的请求URL让你觉得不舒服的话,那么就用mod_rewrite来实现友好URL吧。接下来是作者给这个框架写的实际重写标准代码:

RewriteEngine On

# Change the URI here to whatever you want your homepage to be

RewriteRule ^/$ /index.php?module=welcome [L,QSA]

# Changes /index.php?module=welcome to /welcome

RewriteCond %{DOCUMENT_ROOT}/%{REQUEST_FILENAME} !-d

RewriteCond %{DOCUMENT_ROOT}/%{REQUEST_FILENAME} !-f

RewriteRule ^/([^/]*)$ /index.php?module=$1 [L,QSA]

# Changes /index.php?module=users&class=login to /users/login

RewriteCond %{DOCUMENT_ROOT}/%{REQUEST_FILENAME} !-d

RewriteCond %{DOCUMENT_ROOT}/%{REQUEST_FILENAME} !-f

RewriteRule ^/([^/]*)/([^/]*)$ /index.php?module=$1&class=$2 [L,QSA]

# Changes /index.php?module=users&class=login&event=foo

# to /users/login/foo.html

RewriteCond %{DOCUMENT_ROOT}/%{REQUEST_FILENAME} !-d

RewriteCond %{DOCUMENT_ROOT}/%{REQUEST_FILENAME} !-f

RewriteRule ^/([^/]*)/([^/]*)/([^/]*).html$ \

/index.php?module=$1&class=$2&event=$3 [L,QSA]

Extending the Controller

  【扩展控制器】

  拥有一个集中控制器的一点好处就是你加入一些功能后,马上就能通过控制器体现出来。以下是几个可以扩展一下这个控制器的点子,使这个框架的整体能力更加强大:
 
  你可以使用PHP5里一个新东西SoapServer来自动检测一个请求是否为SOAP

  你可以使用控制器来过滤所有的自动全局变量如$_GET和$_POST以防止恶意HTML代码等

  你可以使用控制器即时地转换表述层,比如从默认的方式转到PDF方式

  你可以直接在控制器中加入缓存机制,这样的好处是应用程序整体都能使用到缓存以提高效率

  当然,需要注意一点的是,你在控制器中所增加的功能将体现在程序全局。如你想过滤所有的自动全局变量,但是很多应用程序的管理员需要使用到一些HTML代码,反而成为一件棘手的事情(译者注:本人的想法是可以加一个if条件语句,在加载特定模块时不应用过滤功能即可)。
(本文被浏览次数: 11 )

]]>

理解PHP中的MVC编程之MVC框架简介

Filed under: Uncategorized — 佳音 @ 8:00 下午

来源/作者:PHP5研究室 Joe Stump 2006-08-22 [
 【什么是MVC?】

  MVC是一个可以让你把“三个部分(即MVC的全称,Model、View、Controller)”谐调地组成一个复杂应用程序的概念。一辆汽车就是一个在现实生活中非常好的MVC例子。我们看车都看两个View(显示)部分:内部和外部。而这两个都离不开一个Controller(控制者):司机。刹车系统、方向盘和其他操控系统代表了Model(模型):他们从司机(Controller)那里取得控制方法然后应用到内部和外观(View)。

  【网络上的MVC】

  MVC框架所涵盖的概念相当简单并且极度灵活。基本的概念就是,你有一个单独的控制器(如index.php)用来控制所有建立在参数请求基础上的框架内应用程序。这个控制器通常包含了(最小程度上)一个定义模型的参数、一个事件和一个GET参数。这样控制器就能确认所有的请求然后运行相应的事件。打个比方来说,一个像这样/index.php?module=foo&event=bar的请求很有可能就是用来载入一个名叫foo的类,然后运行foo::bar()[就是其中的bar()函数]。这样做的好处有:

  一个对应所有应用程序的接口

  同时维护一个应用程序内无数的代码非常麻烦,因为每一段代码都有自己的相对路径、数据库链接、验证等等。而这样做就免除你在这方面的烦恼,允许你合并并重复使用代码

  【为什么要创建作者自己的MVC框架?】

  迄今为止,我没有见到过太多用PHP写的MVC框架。事实上我仅仅知道一个-Solar,是完全用PHP5写的。另外一个是Cake,一个试图成为PHP的RoR(Ruby on Rails-一个Ruby语言开源网络框架)。我自己对这两个框架都有一些不满意的地方:它们都没有利用到PEAR,Smarty等所包含的现有代码;现在的Cake还比较紊乱;最后,Solar是一个绝大部分由一个人写的作品(我无意说其作者Paul不是一个好人或者好程序员)。这些问题可能并不会让你否认它们,而且很有可能你根本不关心这些问题。但是正因为如此,我请各位尽可能地审视它们。

  【老方式】

  如果回到2001看自己写的代码,作者有可能找到一个叫template.txt的文件,它看起来像这样:www.phpv.net 转载请注明出处

<?php
 require_once(‘config.php’); // Other requires, DB info, etc.

 $APP_DB = ‘mydb’;
 $APP_REQUIRE_LOGIN = false; // Set to true if script requires login
 $APP_TEMPLATE_FILE = ‘foo.php’; // Smarty template
 $APP_TITLE = ‘My Application’;

 if ($APP_REQUIRE_LOGIN == true) {
  if (!isset($_SESSION['userID'])) {
   header(“Location: /path/to/login.php”);
   exit();
  }
 }

 $db = DB::connect(‘mysql://’.$DB_USER.’:’.$DB_PASS.’@localhost/’.$APP_DB);
 if (!PEAR::isError($db)) {
  $db->setFetchMode(DB_FETCHMODE_ASSOC);
 } else {
  die($db->getMessage());
 }

 // Put your logic here
 // Output the template

 include_once(APP_TEMPLATE_PATH.’/header.php’);
 include_once(APP_TEMPLATE_PATH.’/’.$APP_TEMPLATE_FILE);
 include_once(APP_TEMPLATE_PATH.’/footer.php’);
?>

  天哪,只是看这些代码都会让我有退缩的欲望。这段代码的概念就是确保每一个应用程序都能适用于这个处理方法,比如我可以简单地将template.txt拷进myapp.php,改变一些变量,瞧,它就能运行起来了。尽管如此,这个组织严密的处理方法存在一些严重的缺点:

  如果我的老板想让作者用myapp.php在一些情况下输出PDF、一些情况下输出HTML、一些情况下(直接提交的XML请求)SOAP,我该怎么办?

  如果这个应用程序需要IMAP或LDAP验证,我该怎么办?

  我该如何处理各种不同的代码(包括编辑、升级和删除)?

  我该如何处理多级验证(管理员 vs. 非管理员)?
我该如何启用输出缓存?www.phpv.net 转载请注明出处

  【新方式】

  将所有东西都扔进这个MVC框架,你会发现生活是如此简单。请对比以下代码:

<?php
 class myapp extends FR_Auth_User
 {
  public function __construct()
  {
   parent::__construct();
  }

 public function __default()
 {
  // Do something here
 }

 public function delete()
 { }

 public function __destruct()
 {
  parent::__destruct();
 }
}

?>

  注意这段代码显然不是用来链接到一个数据库、判断一个用户是否已经登陆、或者输出任何其他信息。控制器掌握了所有的一切。

  如果我想验证LDAP,我可以建立FR_Auth_LDAP。控制器可以识别某些输出方法(比如$_GET['output'])并可以随时转换成PDF或者SOAP。事件处理delete,只负责删除,其他的它都不管。因为这个模块拥有一个FR_User类的实例,它可以简单地判断一个用户是否已经登陆等等。Smarty,作为模板引擎控制缓存是理所当然的,但是控制器同样可以控制一部分缓存。

  从前面讲的老方式到MVC方式对于很多人来讲可能是一个全新、陌生的概念,但是一旦你转换到了这样一个概念,那么要转回去将是件相当困难的事情。
 【建立底层】

  我是一个PEAR尤其是PEAR_Error类的爱好者。PHP5引入了一个新的内建类“Exception” 取代了PEAR_Error。但是PEAR_Error拥有一些比Exception还要实用的特性。所以,在此系列文章中的MVC框架实例将用到它来做错误处理。无论如何,我还是要用到Exception获得从构造器中的错误,因为它们本身不能传回错误。

  设计这些基础类的目的有如下几点:

  利用PEAR快速添加功能到基础类

  建立小巧、可反复实用的抽象类以便让使用者在此框架中快速开发出应用程序

  用phpDocumentor给所有的基础类生成文档

  类的层次看起来会像这样:

  -FR_Object将会提供基础的功能以供其他所有对象使用(包括logging,一般的setFrom(),toArray())

  -FR_Object_DB是一个小层面,给子类提供数据库链接等功能

  -FR_Module是所有应用(又称模块、模型等等)的底层类

  -FR_Auth是所有验证机制的底层类

   ·FR_Auth_User是一个验证类,用来验证所有需要验证用户是否登陆的模块

   ·FR_Auth_No是所有不需要验证的模块的“假验证类”

  -FR_Presenter是所有用来处理载入和显示应用的底层类

  -FR_Presenter_Smarty是包含了载入不同驱动器能力的显示层。Smarty是一个非常好的模板类,它拥有内建的缓存机制以及一个活跃的开发团体(译者注:这分明就是打广告嘛~)

   ·FR_Presenter_debug是调试部分的显示层。依靠它,开发者能够调试应用程序并给他们除错

   ·FR_Presenter_rest是一个可以让开发者能够以XML方式输出应用程序的REST显示层

  从以上的基础类结构上,你应该可以看到这个MVC框架的不同部分。FR_Module提供所有模块所需要的东西,而FR_Presenter则提供不同的显示方法。在此系列文章中的下一篇中,我将创建控制器将这上面所有的基础类结合在一块。

  【代码标准】

  在你正式编写代码之前,应该坐下来跟你的合伙人(或者你自己)好好讨论(或思考)一下代码标准。MVC编程的整体思想围绕着两点:代码的可再利用性(减少偶合)和代码的标准化。我推荐至少应该考虑到如下几点:

  首先要考虑的是变量命名和缩写标准。不要因为这个跟你的合作伙伴大吵一通,但是一旦定下来的标准,就要自始至终地遵从,尤其是写底层代码(基础类)的时候。

  定制一个标准前缀,用在所有的函数、类和全局变量上。不走运的是,PHP不支持“namespace(命名空间)”。所以要想避免混淆变量名和发生的冲突,用一个前缀是个明智的做法。我在整篇文章中将使用“FR_”作为这样的前缀。

  【编写底层】

  文件层次规划很重要。基本的层次规划很简单且在一定程度上是严格定义的:

/
config.php
index.php
includes/
Auth.php
Auth/
No.php
User.php
Module.php
Object.php
Object/
DB.php
Presenter.php
Presenter/
common.php
debug.php
smarty.php
Smarty/
modules/
example/
config.php
example.php
tpl/
example.tpl
tpl/
default/
cache/
config/
templates/
templates_c/

  你可能会想这样的文件层次肯定代表了很多的代码!没错,但是你能够完成它的。在整个系列结束后,你会发现你的编程将会变得更简单并且开发速度会得到很大的提升。

  在文件层次里面,所有的基础类都在includes文件夹内。每一个功能模块,都用一个配置文件,至少一个模块文件和一个模板文件。所有的模块包含在modules文件夹内。我已经习惯了将模板文件放在单独的外部文件夹内,也就是tpl文件夹。

  config.php-中枢配置文件,包含所有的全局配置变量。

  index.php-控制器,在接下来的一篇文章中会详细叙述。

  object.php-所有基础类的底层类,提供绝大部分类需要的功能。FR_Object_DB继承这个类并提供数据库链接。

  结构的基本概念就是,让所有的子类都继承一个中枢类以便它们都共享一些共同的特性。你完全可以把链接数据库的功能放进FR_Object,但是并不是所有类都需要这个功能的,所以FR_Object_DB就有了存在的理由,作者会稍后做出讨论它。

<?php
 require_once(‘Log.php’);

 /**
 * FR_Object
 *
 * The base object class for most of the classes that we use in our framework.
 * Provides basic logging and set/get functionality.
 *
 * @author Joe Stump <joe@joestump.net>
 * @package Framework
 */

 abstract class FR_Object
 {
  /**
  * $log
  *
  * @var mixed $log Instance of PEAR Log
  */

  protected $log;
  /**
  * $me
  *
  * @var mixed $me Instance of ReflectionClass
  */

  protected $me;
  /**
  * __construct
  *
  * @author Joe Stump <joe@joestump.net>
  * @access public
  */

  public function __construct()
  {
   $this->log = Log::factory(‘file’,FR_LOG_FILE);
   $this->me = new ReflectionClass($this);
  }

  /**
  * setFrom
  *
  * @author Joe Stump <joe@joestump.net>
  * @access public
  * @param mixed $data Array of variables to assign to instance
  * @return void
  */

 public function setFrom($data)
 {
  if (is_array($data) && count($data)) {
   $valid = get_class_vars(get_class($this));
   foreach ($valid as $var => $val) {
    if (isset($data[$var])) {
     $this->$var = $data[$var];
    }
   }
  }
 }

 /**
 * toArray
 *
 * @author Joe Stump <joe@joestump.net>
 * @access public
 * @return mixed Array of member variables keyed by variable name
 */

 public function toArray()
 {
  $defaults = $this->me->getDefaultProperties();
  $return = array();
  foreach ($defaults as $var => $val) {
   if ($this->$var instanceof FR_Object) {
    $return[$var] = $this->$var->toArray();
   } else {
    $return[$var] = $this->$var;
   }
  }

  return $return;
 }

 /**
 * __destruct
 *
 * @author Joe Stump <joe@joestump.net>
 * @access public
 * @return void
 */

 public function __destruct()
 {
  if ($this->log instanceof Log) {
   $this->log->close();
  }
 }
}

?>

   auth.php-这是所有验证功能的底层类。它是从FR_Module里面延伸出来的,主要功能是定义一个基本的验证类如何工作。

  跟FR_Module的道理一样,有些类不需要链接到数据库,那么同理,FR_Auth_No就可以被创建应用到不需要验证功能的类上。

<?php
 abstract class FR_Auth extends FR_Module
 {
  // {{{ __construct()
  function __construct()
  {
   parent::__construct();
  }
  // }}}
  // {{{ authenticate()
   abstract function authenticate();
  // }}}

  // {{{ __destruct()

   function __destruct()
   {
    parent::__destruct();
   }
  // }}}
 }

?>

module.php-所有模块的心脏

<?php
 abstract class FR_Module extends FR_Object_Web
 {
  // {{{ properties
  /**
  * $presenter
  *
  * Used in FR_Presenter::factory() to determine which presentation (view)
  * class should be used for the module.
  *
  * @author Joe Stump <joe@joestump.net>
  * @var string $presenter
  * @see FR_Presenter, FR_Presenter_common, FR_Presenter_smarty
  */
  public $presenter = ‘smarty’;
  /**
  * $data
  *
  * Data set by the module that will eventually be passed to the view.
  *
  * @author Joe Stump <joe@joestump.net>
  * @var mixed $data Module data
  * @see FR_Module::set(), FR_Module::getData()
  */

  protected $data = array();

  /**
  * $name
  *
  * @author Joe Stump <joe@joestump.net>
  * @var string $name Name of module class
  */

  public $name;

  /**
  * $tplFile
  *
  * @author Joe Stump <joe@joestump.net>
  * @var string $tplFile Name of template file
  * @see FR_Presenter_smarty
  */

  public $tplFile;

  /**
  * $moduleName
  *
  * @author Joe Stump <joe@joestump.net>
  * @var string $moduleName Name of requested module
  * @see FR_Presenter_smarty
  */

  public $moduleName = null;
  /**
  * $pageTemplateFile
  *
  * @author Joe Stump <joe@joestump.net>
  * @var string $pageTemplateFile Name of outer page template
  */

  public $pageTemplateFile = null;
  // }}}

  // {{{ __construct()
  /**
  * __construct
  *
  * @author Joe Stump <joe@joestump.net>
  */

  public function __construct()
  {
   parent::__construct();
   $this->name = $this->me->getName();
   $this->tplFile = $this->name.’.tpl’;
  }

  // }}}
  // {{{ __default()

  /**
  * __default
  *
  * This function is ran by the controller if an event is not specified
  * in the user’s request.
  *
  * @author Joe Stump <joe@joestump.net>
  */

  abstract public function __default();
  // }}}
  // {{{ set($var,$val)

  /**
  * set
  *
  * Set data for your module. This will eventually be passed toe the
  * presenter class via FR_Module::getData().
  *
  * @author Joe Stump <joe@joestump.net>
  * @param string $var Name of variable
  * @param mixed $val Value of variable
  * @return void
  * @see FR_Module::getData()
  */

  protected function set($var,$val) {
   $this->data[$var] = $val;
  }
  // }}}
  // {{{ getData()

  /**
  * getData
  *
  * Returns module’s data.
  *
  * @author Joe Stump <joe@joestump.net>
  * @return mixed
  * @see FR_Presenter_common
  */

  public function getData()
  {
   return $this->data;
  }
  // }}}
  // {{{ isValid($module)

  /**
  * isValid
  *
  * Determines if $module is a valid framework module. This is used by
  * the controller to determine if the module fits into our framework’s
  * mold. If it extends from both FR_Module and FR_Auth then it should be
  * good to run.
  *
  * @author Joe Stump <joe@joestump.net>
  * @static
  * @param mixed $module
  * @return bool
  */

  public static function isValid($module)
  {
   return (is_object($module) &&
   $module instanceof FR_Module &&
   $module instanceof FR_Auth);
  }
  // }}}
  // {{{ __destruct()

  public function __destruct()
  {
   parent::__destruct();
  }
  // }}}
 }
?>

presenter.php-表述层的核心。

<?php
 class FR_Presenter
 {
  // {{{ factory($type,FR_Module $module)
  /**
  * factory  
  *
  * @author Joe Stump <joe@joestump.net>
  * @access public
  * @param string $type Presentation type (our view)
  * @param mixed $module Our module, which the presenter will display
  * @return mixed PEAR_Error on failure or a valid presenter
  * @static
  */

  static public function factory($type,FR_Module $module)
  {
   $file = FR_BASE_PATH.’/includes/Presenter/’.$type.’.php’;
   if (include($file)) {
    $class = ‘FR_Presenter_’.$type;
    if (class_exists($class)) {
     $presenter = new $class($module);
     if ($presenter instanceof FR_Presenter_common) {
      return $presenter;
     }
     return PEAR::raiseError(‘Invalid presentation class: ‘.$type);
    }
    return PEAR::raiseError(‘Presentation class not found: ‘.$type);
   }
   return PEAR::raiseError(‘Presenter file not found: ‘.$type);
  }
  // }}}
 }

?>

  下一篇里,我将介绍控制器(MVC中的Controller,本文的index.php)的构造。第三篇里,我将介绍表述层(MVC里面的View)。第四篇里,我将用具体模块为例建立一个应用(MVC里面的Module或Model)。

]]>

2007/05/08

魔术方法 __sleep 和 __wakeup

Filed under: Uncategorized — 佳音 @ 11:32 上午

__sleep 和 __wakeup

serialize() 检查类中是否有魔术名称 __sleep 的函数。如果这样,该函数将在任何序列化之前运行。它可以清除对象并应该返回一个包含有该对象中应被序列化的所有变量名的数组。

使用 __sleep 的目的是关闭对象可能具有的任何数据库连接,提交等待中的数据或进行类似的清除任务。此外,如果有非常大的对象而并不需要完全储存下来时此函数也很有用。

相反地,unserialize() 检查具有魔术名称 __wakeup 的函数的存在。如果存在,此函数可以重建对象可能具有的任何资源。

使用 __wakeup 的目的是重建在序列化中可能丢失的任何数据库连接以及处理其它重新初始化的任务。

]]>

php 关于类的一些方法(魔术方法)

Filed under: Uncategorized — 佳音 @ 11:28 上午

1,__construct() 构造方法 当实例化一个对象的时候,这个对象的这个方法首先被调用。

2,__destruct()

2007/05/02

当我在爱你的时候

Filed under: Uncategorized — 佳音 @ 10:56 上午

当我在爱上你的时候,
我不希望你自己一个人走,
陪在你的左右,牵着你的小手。

明亮的路灯是见证,
天上的星星也点头。
想你高高的鼻子,带着卷发的头。

剩下的路,
我陪你走。
不希望你皱眉,不希望你挠头
如果有危险
我在你左不是在你右。

« Newer Posts

Powered by 00RZ