PHP生成二维码单态模式封装类

来源:互联网 发布:骨架软件 编辑:程序博客网 时间:2024/05/20 22:26

PHP生成二维码封装类

简介:

1,php生成二维码,利用单态设计模式进行封装,方便调用。2,里边融合了面向对象的基础知识,几个魔术方法,加深熟练3,如果需要php生成二维码的基础代码,我会把上一篇链接在本篇文章

代码:

<?phpclass makeqrcode{     private static $QRobj;    private $data;    private $level = 'Q';    private $size = '10';    private $margine = '2';    private $filename = '../images/qrcode.png';    private $qrcode_logo = '../images/qrcode_logo.png';    private $rate = 5;    private function __construct($data)    {         require_once '../QRcode/phpqrcode.php';        $this->data = $data;    }    public function __tostring()    {         echo '二维码生成类';    }    public function __set($name,$value)    {         switch($name)        {             case 'level':                if(!($value == 'L' || $value == 'M' || $value == 'Q' || $value == 'H')){                     echo 'level参数错误';                    return;                 }            break;            case 'size':                if( $value <= 0){                     echo '$size参数不能小于等于0';                    return;                }            break;            case 'margine':                if( $value <= 0){                     echo '$margine不能小于0';                    return;                }            break;            case 'rate':                if( $value <= 1){                     echo '$rate不能小于1';                    return;                }            break;            case 'finename' || 'qrcode_logo':                    echo '这两个参数不能修改';                    return;            break;        }        $this->$name = $value;    }    public function __get($name)    {         echo $this->$name;    }    public function __isset($name)    {         if($name == 'filename' || $name == 'qrcode_logo'){             echo '不用测试,必须要有';            return;        }        $bool = isset($this->$name);        return $bool;    }    public function __unset($name)    {         if($name == 'filename' || $name == 'qrcode_logo'){             echo "不能删除";            return;        }        unset($this->$name);    }    public function __call($funname,$param)    {         echo '您调用的方法'.$funname.'不存在';        echo '参数为';        print_r($param);    }     //单态模式生成对象    static function newObj($data)    {         if(is_null(self::$QRobj)){             self::$QRobj = new self($data);        }        return self::$QRobj;    }    //生成纯二维码    public function makeqrcode()    {         QRcode::png($this->data,$this->filename,$this->level,$this->size,$this->margine);    }    //生成夹有logo的二维码    public function qrCodeLogo()    {         $this->makeqrcode();        $QRcode = $this->filename;        $logo = $this->qrcode_logo;        //imagecreatefromstring() 从字符串中的图像流新建一图像        $QR = imagecreatefromstring(file_get_contents($QRcode));        $LG = imagecreatefromstring(file_get_contents($logo));        $QR_width = imagesx($QR);        $QR_height = imagesy($QR);        $LG_width = imagesx($LG);        $LG_height = imagesy($LG);        $QR_LG_width = $QR_width/$this->rate;        $tmp = $LG_width/$QR_LG_width;        $QR_LG_height = $LG_height/$tmp;        $width = ($QR_width-$QR_LG_width)/2;        //从新组合  imagecopyresampled()重采样拷贝部分图像并调整大小         imagecopyresampled($QR,$LG,$width,$width,0,0,$QR_LG_width,$QR_LG_height,$LG_width,$LG_height);        imagepng($QR,'../images/qr_logo_code.png');    }    //清除掉data    public function __destruct()    {         $this->data = "";    }}$obj = makeqrcode::newObj('nihao');$obj->qrCodeLogo();

归纳总结:

面向对象中的魔术方法:

    __set()     当给私有属性设定值的时候会访问该方法,怎么处理方法里设置    __get()     当访问私有属性的时候回访问该方法,自己设置如何处理    __call()    当访问类中没有的方法的时候,调用该方法    __tostring()    __construct()    __destruct()

单态设计模式:

    节约内存,自始至终实例化一个对象。所以不能随便new,用构造方法私有化即可。用成员方法new对象    ,但没对象访问,所以成员方法静态化用类名访问。如何判断到你new没new,可加一个属性来判断,    静态方法不能访问非静态属性,所以对象属性静态化。最后就是静态方法,静态属性。静态方法里判    断静态属性。只能实例化一次。

0 0
原创粉丝点击