1. ?? ??? ??? ??? ?????
??? ?? ??? ??? ??? ??? ?? ??? ???? ??? ?????? ?? ?? ????. ??? ??? ??? ???? ?? ??? ? ???? ?? ? ????. ?? ??? ??? ?? ?? ??? ?? ????? ??? ??, ?? ??? ??? ?? ?? ??? ??? ? ????. ??? ???? ?? ??? ?? ??????. ?? ?? ??????? (??? ??) ??? ?????.
2. ??? ??
-
-
// Database ???? ?? DB ??? ?????.
- class Database{
- // ?? ?? ?? ???? ??
- private static $_instance = null;
-
- // ???? ???? ?? ???? ???? ????.
- private function __construct()
- {
- echo 'constructor';
- }
// ??? ????? ???? ???
- public static function getInstance()
- {
- if (!(self::$_instance instanceof ??????)) {
- self::$_instance = new Database();
- }
-
- return self::$_instance;
- }
- }
$database = Database::getInstance();
- var_dump($database);
-
?? ??
??: ??? ??? ???? ? ?? ????? ??? ? ????. ??? ???? ?? ?? ??? ? ????? ???? ??? ??? ? ??? ? ?? ??? ????? ???? ??? ??? ???? ????(????? ??? ?? ??).
?? ??? ??? ? ????? ????.
-
- ?? ??? {
- private static $_instance = null;
-
- public static function getInstance() {
- $class = __CLASS__;
-
- if(!(self::$_instance ????of $class)) {
- self::$_instance = new $class;
- }
-
- return self:: $_instance;
- }
-
- }
- ??? DB {
- }
-
- ??? DBWriteConnection ?? DB {
- ??? ??;
-
- ???? ?? __construct () {
- echo 'DBWriteConnection
';
- }
- }
-
- class DBReadConnection ?? DB {
- ??? ??;
-
- ?? ?? __construct () {
- echo 'DBReadConnection
';
- }
- }
-
- $dbWriteConnection = DBWriteConnection::getInstance();
- var_dump($dbWriteConnection);
?? ??
3. ????? ??
????? ??? ??? ??? ? ??? ??? ????? ???? ??? ? ?? ????? ??? ? ??? ?? ??? ?? ?? ??????(?? ?? ????? ?? ? ?? ??????).
?? ???:
-
-
class Registry {
- /**
- * @var array ?? ??? ???? ???
- */
- static private $_store = array() ;
-
- /**
- * ?????? ?? ??
- *
- * ??? ???? ??? ??? ??? ?????.
- *
- * @param mix $object ??? ??
- * @param string $name ??? ???? ? ???? ??
- * @return void
- * @throws Exception
- */
- static public function add($object, $name = null)
- {
- // ??? ???? ?? ?? ??? ??? ???? ???? ????????.
- $name = (!is_null($name)) ?$name:get_class($object);
- if (isset(self::$_store[$name])) {
- throw new Exception( "??? ?? ?????? ?????.");
- }
-
- self::$_store[$name]= $object;
- }
-
- /**
- * ??????? ?? ????
- *
- * @param string $name ?? ??, {@see self::set()}
- * @return Mixed
- * @throws ??
- */
- ?? ?? ?? get($name)
- {
- if (!self::contains($name)) {
- throw new Exception("??? ?????? ???? ????.");
- }
return self::$_store[$name];
- }
-
- /**
- * ?????? ??? ??? ??
- *
- * @param string $name ?? ??, {@see self::set()}
- * @return bool
- */
- ?? ?? ???? ??? ?????. ($name)
- {
- if (!isset(self::$_store[$name])) {
- return false;
- }
-
- return true;
- }
-
- /**
- * ??????? ?? ??
- *
- * @param string $name ?? ??, {@see self::set()}
- * @returns void
- */
- ?? ?? ?? ??($name)
- {
- if (self::contains($name)) {
- unset( self::$_store[$name]);
- }
- }
- }
-
-
?? ??
??? ????? Registry ???? ?????.
-
-
require 'Registry.php';
class DBReadConnection {}
- class DBWriteConnection {}
$read = new DBReadConnection;
- Registry::add($read);
$write = new DBWriteConnection;
- Registry::add($write);
// ?? ????? ????? ?????:
- $read = Registry::get( 'DBReadConnection');
- $write = ?????::get('DBWriteConnection');
var_dump($read);
- var_dump($write);
-
-
?? ??
??? ??? ????? ??? ???? ????, ???? ?????? ?? ???? ????.
?? ??:
-
-
require 'Registry.php';
?? ??? DBConnection {
- static public function getInstance($name = null)
- {
- // __CLASS__? ?? ?? ??? ??? ?????.
- $class = get_called_class();
- // ?? ?? ?? ?? ????? ????
- // ??? ???? ??? ????? ?????.
- $name = (!is_null($name)) ? $name:$class;
- if (!Registry ::contains($name)) {
- $instance = new $class();
- Registry::add($instance, $name);
- }
- return Registry::get($ name);
- }
- }
class DBWriteConnection? DBConnection? ?????. {
- public function __construct()
- {
- echo 'DBWriteConnection
- }
- }
class DBReadConnection? DBConnection? ?????. {
- ?? ?? __construct()
- {
- echo 'DBReadConnection
- }
- }
$dbWriteConnection = DBWriteConnection::getInstance('abc');
- var_dump($dbWriteConnection);
- $dbReadConnection = DBReadConnection::getInstance();
- var_dump($dbReadConnection);
-
?? ??
4.?? ??
?? ??? ??? ? ?? ? ???? ??? ????? ??? ?????. ????? ??? ?? ???? ?????? ???? ??? ????? ?? ??? ??? ?????.
????? ?? ??? ?? ???? ??? ??? ??? ???? ?? ?? ??? ????? ? ?? ?????. ?? ?? ??? ??, ?? ?? ?? ???? ??? ????. ??? ??? ?? ? ??? ?? ?? ??? ??? ??? ??? ??? ???? ? ??? ????.
-
- /**
- * ?? ???
- *
- * ??, mysql ?? sqlite ?? ?? ? ??
- */
- class Log_Factory {
- /**
- * ?? ?? ????
- *
- * @param string $type ?? ??? ??, ??, mysql ?? sqlite
- * @param array $options ?? ??? ??
- */
- ?? ?? getLog( $type = 'file', array $options)
- {
- // ??? ???? ???
- $type = strtolower($type);
-
- // ??? ?? ??
- $class = "Log_" .ucfirst($type);
- require_once str_replace('_', DIRECTORY_SEPARATOR, $class) '.php';
-
- // ????????. ???? ???? ??? ??? ??
- $log = new $class($options);
- ???($type) {
- ??? 'file':
- $log->setPath($options[ 'location']);
- break;
- case 'mysql':
- $log->setUser($options['username']);
- $log->setPassword($options ['password']);
- $log->setDBName($options['location']);
- break;
- case 'sqlite':
- $log->setDBPath($ options['location']);
- break;
- }
-
- return $log;
- }
- }
?? ??
5. ?? ??
?? ??? ???? ?? ???? ??? ?? ??? ??? ??? ???? foreach? ??? ??? ? ????. ?? foreach ??? ????? ??? ???? ??? ??? ? ????.
(1) Iterator ????? ??
-
-
class BasicIterator? Iterator? ?????. {
- private $key = 0;
- private $data = array(
- "hello",
- "world",
- );
?? ?? __construct() {
- $this->key = 0;
- }
?? ?? rewind() {
- $this->key = 0;
- }
() {
- return $this->data[$this->key];
- }
?? ?? ?() {
- return $this ->key;
- }
?? ?? next() {
- $this->key ;
- return true;
- } p>
public function valid() {
- return isset($this->data[$this->key]);
- }
- }
$iterator = new BasicIterator();
- $iterator->rewind();
do {
- $key = $iterator- >key();
- $value = $iterator->current();
- echo $key .': ' .$value . PHP_EOL;
- } while ($iterator->next( ) && $iterator->valid());
- $iterator = new BasicIterator();
- foreach ($iterator as $key => $value ) {
- echo $key .': ' .$value . PHP_EOL;
- }
-
?? ??
(2) RecursiveIteratorIterator? ???? ??? ?????.
-
-
$array = array(
- "Hello", // ?? 1
- array(
- " World" // ?? 2
- ),
- array(
- "How", // ?? 2
- array(
- "are", // ?? 3
- "you" / / ?? 3
- )
- ),
- "doing?" // ?? 1
- );
$recursiveIterator = new RecursiveArrayIterator($array);
$recursiveIteratorIterator = new RecursiveIteratorIterator($recursiveIterator);
foreach($recursiveIteratorIterator as $key => $value) {
- echo "??: " . $recursiveIteratorIterator->getDepth() . PHP_EOL;
- echo "?: " . $key . PHP_EOL;
- echo "?: " .$value .
-
-
?? ?? (3)??FilterIterator迭代器實(shí)現(xiàn)過(guò)濾
-
-
class EvenFilterIterator ?? FilterIterator { - /**
- * ?? ?? ??
- *
- * @return bool
- */
- public function accept()
- {
- // ?? ???? ?????
- $iterator = $this->getInnerIterator();
-
- // ?? ?? ?????
- $ key = $iterator->key();
-
- // ?? ? ??
- if ($key % 2 == 0) {
- return true;
- }
-
- false ??;
- }
- }
$array = array(
- 0 => "?????",
- 1 => " Everybody Is",
- 2 => "??",
- 3 => "Amazing",
- 4 => "The",
- 5 => "Who",
- 6 => "Doctor",
- 7 => "Lives"
- );
// ???? ??? ???
- $ iterator = new ArrayIterator($array);
// FilterIterator ???
- $filterIterator = new EvenFilterIterator($iterator);
foreach ($filterIterator as $key => $value) {
- echo $key .': '. $ ? . PHP_EOL;
- }
- ?>
-
復(fù)代碼
(4)RegexIterator迭代器
-
-
- // RecursiveDirectoryIterator ???
- $directoryIterator = new RecursiveDirectoryIterator("./" );
// ????? ???? ?? RecursiveIteratorIterator ??
- $recursiveIterator = new RecursiveIteratorIterator($directoryIterator);
// Createa *Iterator*.php ??? ?? ??
- $regexFilter = new RegexIterator($recursiveIterator, '/(.*?)Iterator(.*?).php$/');
- < p>// ??
- foreach ($regexFilter as $key => $file) {
- /* @var SplFileInfo $file */
- echo $file->getFilename() . PHP_EOL;
- }
-
-
復(fù)主代碼
功能:找到所有的php文件
(4)LimitItertor迭代器,imageSQL中的LIMIT
-
-
// ?? ??
- $array = array(
- 'Hello',
- 'World ',
- '???',
- '??',
- '?',
- '??? ???'
- );
// ??? ???
- $iterator = new ArrayIterator($array);
// ?? 2?? ??? ???? ?? ?? ???? ????
- $limitIterator = new LimitIterator($ iterator, 0, 2);
// ??
- foreach ($limitIterator as $key => $value) {
- echo $key .': '. $ ? . PHP_EOL;
- }
-
復(fù)代碼
6. ??? ??(???)
??? ??? ??? ??????? ?? ???? ??? ? ?? ????? ??? ????? ????.
-
-
/** - * ??? ???
- *
- * ? ???? ???? ??? ???? ??
- * ??(FIFO)?? ??? ??? ? ????.
- */
- ?? ??? {
- / **
- * @var array ???? ??? ?? => ??
- */
- static protected $callbacks = array();
-
- /**
- * ?? ??
- *
- * @param string $eventName ??? ??? ??
- * @param mix $callback Event_Callback ?? Closure? ????
- */
- ?? ?? ?? RegisterCallback($eventName, $callback)
- {
- if (!($callback instanceof Event_Callback) && !($callback instanceof Closure)) {
- throw new Exception("??? ??!");
- }
-
- $eventName = strtolower($eventName);
-
- self::$callbacks[$eventName][] = $callback;
- }
-
- /**
- * ??? ???
- *
- * @param string $eventName ???? ??? ??
- * @param mix $data ???? ?? ???
- */
- static public ?? ???($eventName, $data)
- {
- $eventName = strtolower($eventName);
-
- if (isset(self::$callbacks[$eventName])) {
- foreach (self::$callbacks[$eventName] as $callback) {
- self::callback($callback, $data);
- }
- }
- }
-
- / **
- * ?? ??
- *
- * @param mix $callback Event_Callback ?? Closure? ????
- * @param mix $data ???? ??? ???
- */
- ?? ?? ?? ??($callback, $data)
- {
- if ($callback instanceof Closure) {
- $callback($data);
- } else {
- $callback->run($data);
- }
- }
- }
/**
- * ??? ?? ?????
- *
- * ???? ???? ???? ??
- * ?? this? ???? ???? ??? ? ????
- *. run ????
- * ???? ???? ? ?????.
- */
- interface Event_Callback {
- ?? ?? run($data);
- }
/**
- * ?? ??
- */
- class LogCallback? Event_Callback? ?????. {
- ?? ?? run($data)
- {
- echo "?? ???" .
- var_dump($data);
- }
- }
- Event::registerCallback('save', new LogCallback());
// ?? ??? ??? ???? ??
- ???::registerCallback('save', function ($data) {
- echo "?? ???" . PHP_EOL;
- var_dump($data);
- });
- < ;p>class MyDataRecord {
- public function save()
- {
- // ??? ??
-
- // ?? ??? ???
- Event::trigger('save', array ("Hello", "World"));
- }
- }
// ? ??? ??? ?????
- $data = new MyDataRecord();
- $data->save(); // '??' ???? ??? ?????
-
?? ??
7. ??? ?? ??
??? ?? ??? ???? ???? ? ??? ???? ? ???? ???? ??? ? ????.
-
-
/**
- * ?? ???
- */
- ??? ?? {
- /**
- * @var Log_Engine_Interface
- */
- protected $engine = false;
-
- /**
- * ??? ??? ??
- *
- * @param string $message
- */
- ?? ?? add($message)
- {
- if (!$this->engine) {
- throw new Exception('??? ? ? ????. ??? ???? ?????.');
- }
-
- $data['datetime'] = time();
- $data[' message'] = $message;
-
- $session = Registry::get('session');
- $data['user'] = $session->getUserId();
-
- $this->engine->add($data);
- }
-
- /**
- * ?? ??? ?? ?? ??
- *
- * @param Log_Engine_Interface $Engine
- */
- ?? ?? setEngine(Log_Engine_Interface $engine)
- {
- $this->engine = $engine;
- }
-
- /**
- * ??? ???? ?? ??
- *
- * @return Log_Engine_Interface
- */
- ?? ?? getEngine()
- {
- return $this- >engine;
- }
- }
interface Log_Engine_Interface {
- /**
- * ??? ??? ??
- *
- * @param string $message
- */
- ?? ?? add(array $data);
- }
class Log_Engine_File? Log_Engine_Interface? ?????. {
- /**
- * ??? ??? ??
- *
- * @param string $message
- */
- ?? ?? add(array $data)
- {
- $line = '[' .data('r', $data['datetime']). '] ' .$data['message'] ' ???: ' .$data['user'] . ??>
- $config = Registry::get('site-config');
-
- if (!file_put_contents($config['location'], $line, FILE_APPEND)) {
- throw new Exception("??? ?? ?? ??? ??????.");
- }
- }
- }
$engine = new Log_Engine_File();
$log = new Log();
- $log->setEngine($engine);
// ?????? ??
- Registry::add($log);
-
-
?? ??
??? ???? ??? ??? ???? ????. ?? ?? ???? ???. ?? ?? ???? ??? ?? ??? ?????. ?? ?? ???? ???? ?? ???? ??? ???? ??? ???? ?????? ???? ??? ? ??? ?????.
8. ??-?-????
MVC ?????? ??? Model-View-Controller? ??????? ?? ?? ? ?? ?? ?? ??? ???? ?????.
??-??? ?? ?? ?? ???? ???? ????. ??????, ? ??? ?? ??? ? ????.
? ?? ??? ???? ???? ?? ????? ???? ??? ?????.
????-?????? ??? ???? ???? ??? ?? ?? ??? ???? ??? ???? ??? ?, ?? ???? ?? ??? ????? ?????.
???? MVC ???? ?????:
9. ??? ??
??? ?? ???? ??? ?? ??? ??????.
??? ??? ?? ?? ??:
php ??? ??: ??? ??, ??? ??, ??? ????-
PHP ??? ?? ??? ?? ?? ??-
php ??? ?? ?? ?? ??-
PHP ??? ?? ?? ??? ??(2)-
PHP ??? ?? ??? ??? ??-
PHP ??? ??? ??? ?? ??-
PHP ??? ?? ?? ??? ??-
PHP ??? ?? ?? ??? ??-
PHP ??? ?? ??? ?? ?? ??-
PHP ??? ??? ??? ?? ?? ??-
??? ??? ??? ?? ??, PHP ?? ??? ??-
PHP ??? ?? ? ??? ?? ???-
PHP?? ????? ???? ? ?? ??? ??? ?? ?? ??-
PHP ??? ??? ??? ?? ??-
|