php的单例模式

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
class Factory
{
private static $instance = null;

public static function getInstance(){
if(!(self::$instance instanceof self)){
self::$instance = new self;
}
return self::$instance;
}

//声明为私有,防止被拷贝
private function __clone(){}

}
Share
Posted in Uncategorized at February 28th, 2010. No Comments.

xxtea的php算法实现

<?php
/* XXTEA encryption arithmetic library.
 *
 * Copyright (C) 2006 Ma Bingyao <andot@ujn.edu.cn>
 * Version:      1.5
 * LastModified: Dec 5, 2006
 * This library is free.  You can redistribute it and/or modify it.
 */

function long2str($v, $w) {
    $len = count($v);
    $n = ($len – 1) << 2;
    if ($w) {
        $m = $v[$len - 1];
        if (($m < $n – 3) || ($m > $n)) return false;
        $n = $m;
    }
    $s = array();
    for ($i = 0; $i < $len; $i++) {
        $s[$i] = pack("V", $v[$i]);
    }
    if ($w) {
        return substr(join('', $s), 0, $n);
    }
    else {
        return join('', $s);
    }
}
 
function str2long($s, $w) {
    $v = unpack("V*", $s. str_repeat("", (4 – strlen($s) % 4) & 3));
    $v = array_values($v);
    if ($w) {
        $v[count($v)] = strlen($s);
    }
    return $v;
}

function int32($n) {
    while ($n >= 2147483648) $n -= 4294967296;
    while ($n <= -2147483649) $n += 4294967296;
    return (int)$n;
}

function xxtea_encrypt($str, $key) {
    if ($str == "") {
        return "";
    }
    $v = str2long($str, true);
    $k = str2long($key, false);
    if (count($k) < 4) {
        for ($i = count($k); $i < 4; $i++) {
            $k[$i] = 0;
        }
    }
    $n = count($v) – 1;

    $z = $v[$n];
    $y = $v[0];
    $delta = 0x9E3779B9;
    $q = floor(6 + 52 / ($n + 1));
    $sum = 0;
    while (0 < $q–) {
        $sum = int32($sum + $delta);
        $e = $sum >> 2 & 3;
        for ($p = 0; $p < $n; $p++) {
            $y = $v[$p + 1];
            $mx = int32((($z >> 5 & 0x07ffffff) ^ $y << 2) + (($y >> 3 & 0x1fffffff) ^ $z << 4)) ^ int32(($sum ^ $y) + ($k[$p & 3 ^ $e] ^ $z));
            $z = $v[$p] = int32($v[$p] + $mx);
        }
        $y = $v[0];
        $mx = int32((($z >> 5 & 0x07ffffff) ^ $y << 2) + (($y >> 3 & 0x1fffffff) ^ $z << 4)) ^ int32(($sum ^ $y) + ($k[$p & 3 ^ $e] ^ $z));
        $z = $v[$n] = int32($v[$n] + $mx);
    }
    return long2str($v, false);
}

function xxtea_decrypt($str, $key) {
    if ($str == "") {
        return "";
    }
    $v = str2long($str, false);
    $k = str2long($key, false);
    if (count($k) < 4) {
        for ($i = count($k); $i < 4; $i++) {
            $k[$i] = 0;
        }
    }
    $n = count($v) – 1;

    $z = $v[$n];
    $y = $v[0];
    $delta = 0x9E3779B9;
    $q = floor(6 + 52 / ($n + 1));
    $sum = int32($q * $delta);
    while ($sum != 0) {
        $e = $sum >> 2 & 3;
        for ($p = $n; $p > 0; $p–) {
            $z = $v[$p - 1];
            $mx = int32((($z >> 5 & 0x07ffffff) ^ $y << 2) + (($y >> 3 & 0x1fffffff) ^ $z << 4)) ^ int32(($sum ^ $y) + ($k[$p & 3 ^ $e] ^ $z));
            $y = $v[$p] = int32($v[$p] – $mx);
        }
        $z = $v[$n];
        $mx = int32((($z >> 5 & 0x07ffffff) ^ $y << 2) + (($y >> 3 & 0x1fffffff) ^ $z << 4)) ^ int32(($sum ^ $y) + ($k[$p & 3 ^ $e] ^ $z));
        $y = $v[0] = int32($v[0] – $mx);
        $sum = int32($sum – $delta);
    }
    return long2str($v, true);
}
?>

Share
Posted in php at February 7th, 2010. 3 Comments.

php的静态特性

在php6之前,也许你可能会写过这样的代码

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
class a{
    static public function sayA(){
        echo "A";
    }
    static public function query(){
        self::sayA();
    }
   
}
class b extends a{
   
    static public function sayA(){
        echo 'a A from b'; 
    }
}
b::query();

但是觉得很惊奇,你并没有得到 a A from b,这个结果当然不是你所期望的,这个是因为php的静态绑定的结果。在php6中,这一切都得到了改善。和你以前所用的重载一样。

Share
Posted in Uncategorized at February 7th, 2010. No Comments.

php重载的一个用法

     在php中,实现了变量的get和set方法的重载,在5.1.0之后又实现了两个方法的重载,分别为__isset()和__unset()。注意这些方法不能被声明为static方法。

     而在方法的重载上提供了__call(string name, array arguments )函数,该函数将会返回被执行方法的返回值,其调用是在函数在该类中未被找到之后。

     可能这么讲比较抽象,更直观的方式是,我们找一个实际中的应用。在java之中,会经常使用到setter和getter方式。比如,setData() 就是设置一个data变量的值来实现。那么在php中我们可以这么做。我们可能需要设置一个类似存储器的东西,来管理我们放置类中的所有变量。那么我们可以这么实现。

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
 public function __call($method, $args)
    {
        switch (substr($method, 0, 3)) {
            case 'get' :
 
                $key = $this-&gt;_underscore(substr($method,3));
                $data = $this-&gt;getData($key, isset($args[0]) ? $args[0] : null);

            case 'set' :


                $key = $this-&gt;_underscore(substr($method,3));
                $result = $this-&gt;setData($key, isset($args[0]) ? $args[0] : null);
                return $result;

        }
    }

       事实上在那么可以很轻易的把一些关键数据放置于data之中。甚至在你需要的缓存它们。

Share
Posted in php at February 5th, 2010. No Comments.