本节课对面向对象基础与MySQLi对象编程进行了学习
问答: 什么类,什么是对象,举例说明;
类和对象(class)是两种以计算机为载体的计算机语言的合称。对象是对客观事物的抽象,类是对对象的抽象。类是一种抽象的数据类型。它们的关系是,对象是类的实例,类是对象的模板。对象是通过new className产生的,用来调用类的方法;类的构造方法 。
2. 编程: 参考object/demo3.php,自定义类与实例化,要求必须将属性私有化,通过公共接口__set()和__get()进行访问(必须写5遍以上)
实例
<?php /* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ //感恩 //认真的人该变了自己,坚持的人改变了命运! //日行一善,改变命运! class goodTeacher{ private $name; private $age; //优点 private $nature; private $data=[]; public function __construct($name,$age, array $nature) { $this->name=$name; $this->age=$age; $this->nature=$nature; echo $this->age,'<br>'; } public function __get($name){ $msg=null; if(isset($this->$name)){ $msg=$this->$name; }elseif (isset ($this->data[$name])) { $msg= $this->data[$name]; }else{ $mag="无此属性。"; } return $msg; } public function __set($name,$value){ $this->name=$value; } } $teacher=new goodTeacher('老法师',92,[1,2,3,4]); echo $teacher->name,'<br>'; echo $teacher->age, '<br>'; $teacher->age=90; echo $teacher->age, '<br>';
运行实例 »
点击 "运行实例" 按钮查看在线实例
实例
<?php /* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ //感恩 //认真的人该变了自己,坚持的人改变了命运! //日行一善,改变命运! class Teacher2{ private $name; private $age; private $nature; private $data=[]; public function __construct($nameVar,$ageVar,array $natureVar){ $this->name=$nameVar; $this->age=$ageVar; $this->nature=$natureVar; echo $this->age, '<br>'; } public function __get($nameVar){ $msg=null; if(isset($this->$nameVar)){ $msg=$this->$nameVar; }elseif(isset($this->data[$nameVar])){ $msg= $this->data[$nameVar]; }else{ $msg="无此属性"; } return $msg; } public function __set($nameVar,$valueVar){ $this->$nameVar=$valueVar; } } $teacher = new Teacher2('老法师', 91, [1, 2, 3, 4]); echo $teacher->name, '<br>'; echo $teacher->age, '<br>'; $teacher->age = 90; echo $teacher->age, '<br>';
运行实例 »
点击 "运行实例" 按钮查看在线实例
3. 编程: MySQL常用的增删改查语句(CURD),每个语句必须写10遍以上;
实例
INSERT INTO `staff` (`name`,`salary`) VALUES ('夫子',10000); UPDATE `staff` SET `name`='子路',`salary`=9999 WHERE `staff_id`=8; UPDATE `staff` SET `name`='子贡',`salary`=9999 WHERE `staff_id`=10; SELECT * FROM `staff`; SELECT `name`,`salary` FROM `staff` WHERE `salary`>6000; SELECT 20*30 AS result; //计算个数 SELECT COUNT(*) AS RES FROM `staff` ; UPDATE `staff` SET `name`='灭绝师太',`sex`=1 WHERE `staff_id`=7;
运行实例 »
点击 "运行实例" 按钮查看在线实例
4.编程: 数据库的连接与检测(写5遍以上)
实例
<?php //感恩 //认真的人该变了自己,坚持的人改变了命运! //日行一善,改变命运! error_reporting(E_ALL ^E_WARNING); $db_host='127.0.0.1'; $db_user='root'; $db_passwd='root123'; $db_name='php'; $db_charset='utf8'; $mysqli=new mysqli($db_host, $db_user, $db_passwd, $db_name); if($mysqli->connect_errno){ die('连接错误'.$mysqli->connect_errno.':'.$mysqli->connect_error); } echo '<h1>连接成功</h1>';
运行实例 »
点击 "运行实例" 按钮查看在线实例
5、本节课学习了对象基础与MySQLi对象编程。
对Mysqli语句有了较为简略的认识,后面还要加强学习力度。