什么是面向对象?
我们面对的一切都是对象 ,对象是万物
类的实例化就是对象
继承就是当子类可以继承父类
封装
就是可以把多种方法封装到一个逻辑单元里面
class db{ protected $host; protected $user; protected $pas; protected $dbname; protected $db; // 数据库初始化 function __construct($host = DB_HOST,$user = DB_USER,$pas = DB_PASSWORD,$dbname = DB_DBNAME){ $this->host = $host; $this->user = $user; $this->pas = $pas; $this->dbname = $dbname; if(!$this->db){ $this->conn(); } } // 连接数据库 function conn() { $conn = new mysqli($this->host,$this->user,$this->pas); if ($conn->connect_error) { echo "数据库连接失败,错误信息:" . $conn->connect_error; } $conn->select_db($this->dbname); $conn->set_charset("utf8"); $this->db = $conn; }
以下就是可以写方法 封装一个方法