php
文章平均质量分 74
hahaliu005
这个作者很懒,什么都没留下…
展开
-
register pattern
<br />Use register you can register some object in the same array and use get() function to return this object in the object array.<br />this is a simple example below:<br /><?php/** * a example for * register pattern * */class register{ priv原创 2011-05-20 17:03:00 · 336 阅读 · 0 评论 -
session_unset and session_destroy
<br /><br />You can use session begin with session_start();<br />And then if you want to use session_start() again , it is not allowed;<br />To start another session ,you can use session_destroy() and session_unset() first.<br /><br /><br />ses原创 2011-04-09 13:29:00 · 393 阅读 · 0 评论 -
Test driven development and The value object pattern
Test driven development<br />With the value object pattern , First we should write a test file , and if use Zend Studio Editer we can generate test file from file | new | PHPUnit Test Case . Then the test file about four operations just like this:<br原创 2011-04-10 02:01:00 · 345 阅读 · 0 评论 -
Five kinds of sort algorithm
<br />1.Insertion sort<br />Insert a data element to the new array every time , keep the new array still in order.<br />The insertion sort just like this :<br />Beware:I use php to achieve the sort function from small to large,for convenient I use few php原创 2011-04-11 23:34:00 · 337 阅读 · 0 评论 -
Memcache
<br />First you shoule install memcache , this is for windows , memcached-1.2.1-win32.zip in my 163 email .<br />decompress.<br />run : memcached.exe -d install memcached.exe -d start<br />Then get a PHP extension php_memcache.dll , php_memcache.rar in原创 2011-05-30 13:58:00 · 2849 阅读 · 0 评论 -
php socket
<br />Write a simple socket about S/C peers . With this simple code . we can associate more.<br /> <br />server.php file<br /><br /><?php$protocol = getprotobyname('tcp');//create a socket$socket = socket_create(AF_INET,SOCK_STREAM,$protocol);socke原创 2011-04-11 23:51:00 · 269 阅读 · 0 评论 -
The factory pattern
<br />In the factory pattern , it just like a factory , provide what you need . <br />This example imitate the phone factory , it produce three kind of phone .<br />/** * The abstract class phone * */abstract class phone{ protected $_name; prot原创 2011-04-13 23:32:00 · 228 阅读 · 0 评论 -
Singleton Pattern
The Singleton Patter only instance the class one time , after that , we actually use the first instance object all the time.class DbConn { /** * static property to hold singleton instance */ static $instance = false; /** * constructor * p原创 2011-04-14 00:20:00 · 305 阅读 · 0 评论 -
BOM header
When you use some editer like notepad etc to edit file and save it to encoding utf-8 , the editer will add a flag in the front of the file , use hexadecimal express was EF BB BF . So convenient in some condition , but in some PHP file , it will occur some原创 2011-04-16 16:22:00 · 1378 阅读 · 0 评论 -
Get relative path
This function get the relative path , and it can worked in the windows operation system correctly , but in the other operation system like linux or others , it may had some problems , in addition , I don't test it in other system at all ./** * get the re原创 2011-04-17 15:47:00 · 708 阅读 · 0 评论 -
Thumbnail Generator
Use this function can generator a new image with any size , though it's sounds like this , don't use this function to generator the image that width and height big than original image , it will anamor原创 2011-10-27 10:33:36 · 591 阅读 · 0 评论 -
List all the position that two kings not encounter in Chinese chess
Title like , there are many ways to resolve this question , but if request you can only use one variable , how to do it . For use one variable to resolve this question , we can set a eight bit binar原创 2011-11-09 15:38:38 · 325 阅读 · 0 评论 -
refresh a page
<br />If you want to redirect an user and tell him he will be redirected, e. g. "You will be redirected in about 5 secs. If not, click here." you cannot use header( 'Location: ...' ) as you can't sent any output before the headers are sent.<br /><br />转载 2011-04-09 13:25:00 · 244 阅读 · 0 评论 -
about empty() in php
<br /><br />For empty() you must set var like this:<br />$var = 'value':<br />empty($var);<br />If you use it directly like this :<br />empty('value');<br />It is error!<br />the $var be set under will return TRUE:<br />"" (an empty string)0 (0 as an integ原创 2011-04-09 13:10:00 · 256 阅读 · 0 评论 -
Strategy Pattern
<br />The strategy pattern use different ways to deal with different things:<br /><?php /** * a simple strategy pattern */class strategy{ function __construct(){ } /** * if a and b are string will call stradd function * if a and b are n原创 2011-05-23 11:02:00 · 216 阅读 · 0 评论 -
Iterator Pattern
<br />Just like next,prev,current operator function in array , it's iterator , and we can write our iterator class by ourselfse : <br /><?php class iter{ private $container; function add($name,$obj){ $this->container[$name] = new $obj; } funct原创 2011-05-23 12:29:00 · 235 阅读 · 0 评论 -
Observer Pattern
<br />In this example , when trigger error it will call FileErrorLogger and EmailErrorLogger:<br /><?php/** * * The observer pattern example * */class ErrorHandler { var $_observers = array (); var $_error_info; function attach($observer原创 2011-05-24 15:24:00 · 285 阅读 · 0 评论 -
Proxy Pattern
<br />Proxy Pattern is so simple , for this example , you can unstand it :<br /><?php class client{ function client(){ echo "init client object<br />"; } function get(){ echo "client function get<br />"; } function set(){ echo "client原创 2011-05-26 10:21:00 · 240 阅读 · 0 评论 -
Decorator Pattern
<br />The code below collate from the book named PHP pattern , this may be useful when study decorator pattern:<br /><?php class Widget { function paint() { return $this->_asHtml (); }}class TextInput extends Widget { var $name; var $val原创 2011-05-26 11:22:00 · 232 阅读 · 0 评论 -
Recursion all file direction in a direction
Get all file direction and save it to an reference array, The function like below.function scanfile($dir,&$dir_array){ if (is_dir($dir)){ $scandirs = scandir($dir); foreach ($scandirs as $scandir){ if ($scandir != '.' and $scandir != '..'){原创 2011-04-08 12:56:00 · 344 阅读 · 0 评论 -
About function quote parameter in PHP
<br />In general , we can use quote in function like this .<br />function index1(&$test){ $test .= 'inner';}$test = 'outer';index1($test);echo $test;//output: outerinner <br />we can see the outer variable test be changed.<br /> <br />Then we cha原创 2011-05-26 18:26:00 · 290 阅读 · 0 评论 -
Normative Pattern
<br />Though this code is valid , I still can't use it more clearly , Maybe some day check back this code more time , I can unstande how to use it :<br /><?php/** * normative pattern example */class Traveler { public $min_temp;}class Destinat原创 2011-05-24 17:29:00 · 271 阅读 · 0 评论 -
XML-RPC
<br />With XML-RPC extension in PHP , you can use this library to buile a data interaction between client side and server side , it use XML to get or response data .<br /> <br />rpc_client.php<br /><?php/** * The function which supply to connect to ser原创 2011-05-25 14:56:00 · 415 阅读 · 0 评论 -
Adapter Pattern
<br />When you want to use a new version with different function in it , you can use adapter pattern :<br /><?php //version1class version1{ function hello(){ return "hello "; } function world(){ return "world!!"; }}//version2class v原创 2011-05-26 13:51:00 · 201 阅读 · 0 评论 -
A simple maze function
I use php write a maze function , it based on algorithm of random travel graph .array( *this key defines the row * [5]=>array( *this key defines the col * [0]=>1, *top: the value 1 defines the directi原创 2011-04-08 14:36:00 · 318 阅读 · 0 评论 -
buffer control
There are various kinds of buffer. buffer in PHP , in apache server , and in brower .There are something diferences.In PHP there are some function and option about buffer for example: ob_start().etc原创 2011-04-09 13:35:00 · 391 阅读 · 0 评论