Object-oriented database operations package base ----

Thinking: now have a lot of object-oriented related content, but when wrapper class? How package? If a class member?

Introduction: In fact, the package did not like how complex, but requires a skilled process to determine what kind of things you can put in, what kind of form with an equal. Let's practice under the comprehensive package through the operation of a database

 

 

Class master database package

Is defined: a package database operations, i.e. the operation of the database needs to confirm that the database operations on how what kind of functions, and the functions implemented.

1. A class is usually a file, it must first determine the name of the file: In general, there are two class file naming convention.

  •   File name with the class name, such as Sql.php
  •   To distinguish normal php file, add intermediate class description as Sql_class.php
  •   Php now become almost all object-oriented, it is generally the first approach: Thus this data class file naming convention is: Sql_php

2. Make sure the class file name in fact, is to determine the class name, so you can create a Sql class.

<? PHP
 // database operations 
class the Sql { 

}


 ?>

3. The class is created in two ways: First, all the contents of a specific use, that is, inside the class only for a particular use; the second is universal, namely tools, after a lot of places you can use;

  Specific use, different functions can be too flexible

  Common tools, functions should be popular, data changes will be more

After the local database class who want to operate the database can be used to get a lot of projects will be used, so it should be a generic class tools, to consider the characteristics of its available everywhere, let flexibility.

4. The basic operation of the database fault characteristic does not change, i.e., to be connected is connected to the authentication information for authentication is flexible, it can be controlled by setting the attribute information is also used which are different in the different

It should be changed so that incoming data can be achieved by __construct

? < PHP
 // database operations 
class the Sql { 

    // set properties 
    public  $ Host ;
     public  $ User ;
     public  $ pwd ;
     public  $ Port ;
     public  $ dbname ;
     public  $ charset ;
     // constructor initialization data, more data, should be used to pass data arrays, associative array, most of the developers and the intention is to test, 
    // so basically locally to the default data can be 

    public  function the __construct ( array  $ info = array ()) 
    { 
        / / ?? herein is expressed as $ a = $ c ?? $ b is equivalent to $ a = isset ($ c) $ c:? $ b; c exists if the variable c is equal to or equal to b
        $this->host=$info['host']??'localhost';
        $this->user=$info['user']??'root';
        $this->pwd=$info['pwd']??'root';
        $this->port=$info['port']??'3306';
        $this->dbname=$info['dbname']??'t1';
        $this->charset=$info['charset']??'utf8';
    }
}

$info =array(0=>'sdfsdf',1=>'sdf',2=>'sdfsdf');
print_r($info);

?>

Note: Method set of principles is only one way to achieve a simple function, do not accumulate multiple functions into a method.

The database is automatically initialized when the property is instantiated object sql

 

Guess you like

Origin www.cnblogs.com/xiaowie/p/12237747.html