Mysql热备来实现读写分离,setter基类的代码_php技巧_脚本之家

 必赢亚州手机网站     |      2019-11-23 01:22

PHP3、PHP4都富有类,但它们的类定义的骨子里十分不像样,功效还挺难为情的,但质感上说PHP5重新组织了面向对象的扶植,尽管并不是全然面向对象,但也算能拿出去见人了。 前些天早上闲着粗俗便弄起那东西,认为PHP5扩大的类成员权限关键字蛮好,但难题又来了,就好像尚未大器晚成种便利的章程得以定义字段的getter以致setter,古板的章程是那样定义的: classa { private$田野先生; publicfunctionget_field(){return$this->$field;} publicfunctionset_田野先生{$this->田野=$value;} } 尽管完结起来挺轻松,然而说实在的,为三个字段去写这一堆代码还真不爽。。 于是便研商着是还是不是有风华正茂种更实惠的不二法门来解决,而且能够一本万利地定义它的品类约束什么的。 捣鼓了半天,终于弄出八个类来解决这么些标题: classabstract_entity { private$fields; private$sys_type=array( "bool"=>"", "array"=>"", "double"=>"", "float"=>"", "int"=>"", "integer"=>"", "long"=>"", "null"=>"", "object"=>"", "real"=>"", "resource"=>"", "string"=>"" //"mixed"and"number" ); protectedfunction__construct { /********************************* *$fields=array( *"id"=array( *"allow_null"=false, *"value"=1, *"type"="int" *); *); **********************************/ $this->fields=$fields; } publicfunction__get { if(array_key_exists { return$this->fields[$key]["value"]; } else { thrownewException; } } publicfunction__set { if(array_key_exists { $allow_null=$this->fields[$key]["allow_null"]; $type=$this->fields[$key]["type"]; if(array_key_exists($type,$this->sys_type)) { $fun=create_function('$value',"returnis_$type; if { $this->fields[$key]["value"]=$value; } elseif($allow_null&&is_null { $this->fields[$key]["value"]=NULL; } else { thrownewException("该值类型不科学,必须为".$type."类型"); } } elseif { if { $this->田野(field)s[$key]["value"]=$value; } elseif { $this->fields[$key]["value"]=NULL; } else { thrownewException; } } elseif { if||is_float { $this->fields[$key]["value"]=$value; } elseif&&$allow_null) { $this->fields[$key]["value"]=NULL; } else { thrownewException("该值类型不科学,必需为".$type."类型"); } } else { if||interface_exists { if(is_subclass_of { $this->fields[$key]["value"]=$value; } elseif&&$allow_null) { $this->fields[$key]["value"]=NULL; } else { thrownewException("该值类型不得法,必得为".$type."类型"); } } elseif&&$allow_null) { $this->fields[$key]["value"]=NULL; } } } else { thrownewException; } } } 通过定义三个决然格式的array能够比较实惠地定义该字段的类型、是还是不是允许NULL值以致暗许值。 测量试验代码如下: classtestextendsabstract_entity { publicfunction__construct() { $define=array( "id"=>array( "allow_null"=>false, "value"=>1, "type"=>"int" ), "name"=>array( "allow_null"=>false, "value"=>"abc", "type"=>"string" ), "gender"=>array( "allow_null"=>false, "value"=>true, "type"=>"bool" ), "ins"=>array( "allow_null"=>false, "value"=>$this, "type"=>"test" ), "ins1"=>array( "allow_null"=>true, "value"=>$this, "type"=>"test" ), "ins2"=>array( "allow_null"=>true, "value"=>NULL, "type"=>"config_media_type" ) ); parent::__construct; } } $a=newtest(); $a->id=123; eche$a->id; echo$a->ins1; $a->ins1=NULL; echois_null; 这里边落实了getter甚至setter,但由于时间涉及笔者没去达成readonly的成效,其实很简短,就是再加风姿洒脱项,标记它能还是不可能被改写就成

兑现cakePHP读写分离,首先配置数据库连接
app/Config/database.php
class DATABASE_CONFIG {
    //可读可写权限
    public $default = array (
        'datasource' => 'Database/Mysql',
        'persistent' => false, // 是不是选取持久化
        'host' => 'localhost',
        'login' => 'root',
        'password' => '1234',
        'database' => 'shop',
        // 'prefix' => 'app_',
        'encoding' => 'utf8'
    );
//只读权限
    public $readonly = array (
        'datasource' => 'Database/Mysql',
        'persistent' => false, // 是不是使用悠久化
        'host' => 'localhost',
        'login' => 'root2',
        'password' => '123456',
        'database' => 'shop',
        // 'prefix' => 'app_',
        'encoding' => 'utf8'
    );
    }
下一场改正model的父文件 app必赢亚州手机网站,ModelAppModel.php,
 
/**
 * Application model for Cake.
 *
 * This file is application-wide model file. You can put all
 * application-wide model-related methods here.
 *
 * PHP 5
 *
 * CakePHP(tm) : Rapid Development Framework ()
 * Copyright 2005-2012, Cake Software Foundation, Inc. ()
 *
 * Licensed under The MIT License
 * Redistributions of files must retain the above copyright notice.
 *
 * @copyright     Copyright 2005-2012, Cake Software Foundation, Inc. ()
 * @link          CakePHP(tm) Project
 * @package       app.Model
 * @since         CakePHP(tm) v 0.2.9
 * @license       MIT License ()
 */
App::uses('Model', 'Model');
/**
 * Application model for Cake.
 *
 * Add your application-wide methods in the class below, your models
 * will inherit them.
 *
 * @package       app.Model
 */
class AppModel extends Model {
    //归拢为一个sql 插入数据库
//(假设是字符串类型,必得超前用单引号把内容引起来,否则当成了int类型管理卡塔 尔(阿拉伯语:قطر‎
    public function createMany($data){
        $ca_sql="INSERT INTO ".$this->useTable;
        $ca_sql_arr=array();
        $keys=array();
        foreach($data as $k=>$v){
            if(empty($keys)){
                $a_keys = array_keys($v);
                foreach($a_keys as $kv){
                    $keys[]="`".$kv."`";
                }
            }
            $ca_sql_arr[]="(".implode(",", $v).")";
        }
        if(!empty($ca_sql_arr)){
            $sql=$ca_sql." (".implode(",", $keys).")  VALUES".implode(",", $ca_sql_arr);
            $this->query($sql);
        }
    }

上一篇:没有了 下一篇:发布一个用PHP,带详细注释