Correction status:qualified
Teacher's comments:
0829作业
第一题:
<?php class person { public $name = '南柱赫'; public $age = 18; public $sex = 'male'; public function getinfo($name = '宋仲基', $age = '20', $sex = 'male') { $this->name = empty($name) ? $this->name : $name; $this->age == ($age = 0) ? $this->age : $age; $this->sex = empty($sex) ? $this->sex : $sex; return '姓名:' . $this->name . ', 年龄:' . $this->age. ',性别:' . $this->sex.'<br>'; } }
点击 "运行实例" 按钮查看在线实例
<body> <style> .box{ display:block; width: 400px; height: 200px; background-color: lightgreen; } .box1{ width: 400px; height: 20px; background-color: lightcoral; } .box2{ width: 400px; height: 180px; } </style> <div class="box"> <div class ="box1"> 作业第一题:问答:什么是类,什么是对象,举例说明? </div> <div class ="box2"> 类其实就是一个抽象的概念,而对象是类抽象概念的具体表达, 就比如人是一个抽象的概念,但是人都有名字,年龄,性别, 长得好不好,长头发还是短头发,身高体重是多少等等,属于人身上的属性, 再比如你是一位男生要去相亲, 然后你还没见过你相亲对象,你只知道是一个人,你就会好奇她的年龄, 长得好不好,长头发还是短头发,身高体重是多少等等的相关信息, 所以对象其实就是类的具体表达。 </div> </div> </body> <?php // 导入类 include 'class/person.php'; // 实例化类,创建对象的过程 $person = new person(); echo '<hr>'; var_dump($person); echo '<hr>'; // 访问类中成员中的属性变量 echo $person->name,'<hr>'; echo $person->age,'<hr>'; echo $person->sex,'<hr>'; // 访问类中成员中的方法函数 echo $person->getinfo(),'<br>';
点击 "运行实例" 按钮查看在线实例
第二题:编程,参考object/demo3.php
<?php class person1 { //访问控制: private private $name; //年龄 private $age; //性别 private $sex; // 三维 private $stature = []; //属性收集器 private $data = []; // 声明构造方法: 对象属性的初始化,在类实例化的时候,自动调用 public function __construct($name, $age, $sex, array $stature) { // private 访问符限制的属性仅在当前对象内部可以使用 $this->name = $name; $this->age = $age; $this->sex = $sex; $this->stature = $stature; } /*1.创建对外访问的公共接口 * 2.类中用双下划线的方法是系统定义,由系统自动调用,叫魔术方法 *3.__get()这个方法用来获取私有成员属性值的,有一个参数就是$name, * 用来获取的成员属性的名称(就是之前定义的私有属性:姓名,年龄,性别等),并返回获取的属性值, */ public function __get($name) { $msg = null; if (isset($this->$name)) { $msg = $this->$name; } elseif (isset($this->data[$name])) { $msg = $this->data[$name]; } else { $msg = '无此属性'; } return $msg; } //__set()方法:这个方法用来为私有成员属性设置值,设置器 public function __set($name,$value) { $this->$name= $value; } } /* 第二遍: public function __get($name2) { $msg = null; if (isset($this->$name2)) { $msg = $this->$name2; } elseif (isset($this->data[$name2])) { $msg = $this->data[$name2]; } else { $msg = '无此属性'; } return $msg; } //__set()方法:这个方法用来为私有成员属性设置值 public function __set($name2,$value) { $this->$name2= $value; } } 第三遍: public function __get($name3) { $msg = null; if (isset($this->$name3)) { $msg = $this->$name3; } elseif (isset($this->data[$name3])) { $msg = $this->data[$name3]; } else { $msg = '无此属性'; } return $msg; } //__set()方法:这个方法用 public function __set(,$value)来为私有成员属性设置值 $name3 { $this->$name3= $value; } } 第四遍: public function __get($name4) { $msg = null; if (isset($this->$name4)) { $msg = $this->$name4; } elseif (isset($this->data[$name4])) { $msg = $this->data[$name4]; } else {性'; } $msg = '无此属 return $msg; } //__set()方法:这个方法用来为私有成员属性设置值 public function __set($name4,$value) { $this->$name4= $value; } } 第五遍: public function __get($name5) { $msg = null; if (isset($this->$name5)) { $msg = $this->$name5; } elseif (isset($this->data[$name5])) { $msg = $this->data[$name5]; } else { $msg = '无此属性'; } return $msg; } //__set()方法:这个方法用来为私有成员属性设置值 public function __set($name5,$value) { $this->$name5= $value; } } */
点击 "运行实例" 按钮查看在线实例
<?php require 'class/Person1.php'; $Person1 = new Person1('范冰冰',18,'female',[98,128,188]); echo $Person1->name,'<hr>';//范冰冰 echo $Person1->age,'<hr>';//18 echo $Person1->sex,'<hr>';//female $Person1->name = '李冰冰';//将之前的name为范冰冰设置成为name为李冰冰 echo $Person1->name,'<hr>';//李冰冰 $Person1->age = 28;//将之前的age为18设置成28 echo $Person1->age = 28,'<hr>';//28 echo $Person1->email,'<hr>';//无此属性
点击 "运行实例" 按钮查看在线实例
第三题:mysql常用的增删改查(CURD)
3. 编程: MySQL常用的增删改查语句(CURD),每个语句必须写10遍以上; 这个的语句在数据库中,由于表有限,不能全部删完,所以不够10遍的, 我在这里又写了一下,这个文件试运行不了的 运行的在数据库中,这些就是我写数据库里面基本内容 增(insert) insert into staff (name) values ('a1'); insert staff(name,salary) values('b',2345); insert into staff(name,salary) values('c',4567); insert into staff(name,salary) values('c',4567); insert into staff(name,salary) values('d'7686); insert staff(name,salary) values('f'5686); insert into staff(name,salary) values('g'7686); insert staff(name,salary) values('h'9686); insert into staff(name,salary) values('l'7686); insert into staff(name,salary) values('c',4567); insert into staff(name,salary) values('d',4567); insert into staff(name,salary) values('f',4567); insert staff(name,salary) values('g',4699); insert into staff(name,salary) values('v'7766); insert into staff(name,salary) values('4534',7766); 更新(改update) update staff set age=30,salary=9090 where id=20; update staff set age=25,salary=7090 where id=11; update staff set age=55,salary=6868 where id=1; update staff set age=36,salary=1200 where id=19; update staff set age=19,salary=4800 where id=23; update staff set age=34,salary=4560 where id=26; update staff set age=21,salary=8888 where id=15; update staff set sex=1,salary=8989 where id=25; update staff set age=35,salary=8989 where id=10; update staff set sex=1,salary=7687 where id=27; 查(select) SELECT * FROM staff; SELECT id,name,age,salary from staff where age>25; SELECT * from staff where sex !=0; SELECT * from staff where age!=25; select 14*50 as result; select 40*60 as result; SELECT * FROM staff; SELECT * from staff limit 5; select count(*) as res from staff; select id,name from staff where age>30; 删(delete) delete from staff where id=11; delete from staff where id=19; delete from staff where id=20; delete from staff where id=24; delete from staff where id=22; delete from staff where id=25; delete from staff where name='c'; delete from staff where id=10; delete from staff where id=7; delete from staff where id=5; delete from staff where id=3;
点击 "运行实例" 按钮查看在线实例
增:
查;
4.数据库的连接
<?php //echo '<h2>创建数据库连接方法一:将连接参数转为数组</h2>>' $db = [ 'host' => '127.0.0.1', 'user' => 'root', 'pass' => 'root', 'name' => 'php', 'charset' => 'utf8', ]; //echo '<h2>创建数据库连接方法二:将连接参数转为变量</h2>>' //$db_host = '127.0.0.1'; //$db_user = 'root'; //$db_pass = 'root'; //$db_name = 'test'; //$db_charset = 'utf8';
点击 "运行实例" 按钮查看在线实例
<?php echo '<h2> 简化: 将连接参数转为数组或参数 </h2>'; //创建一个数据库连接,并返回mysqli对象 require 'config.php'; //设置错误级别 error_reporting(E_ALL ^E_WARNING); echo '<h4>创建数据库连接方法一:将连接参数转为数组<h4>'; $mysqli = new mysqli($db['host'],$db['user'],$db['pass'], $db['name']); //echo '<h4>创建数据库连接方法二:将连接参数转为参数<h4>'; //$mysqli = new mysqli($db_host,$db_user,$db_pass, $db_name); //判断是否连接成功?mysqli_errno() 函数返回最近调用函数的最后一个错误代码。 if ($mysqli->connect_errno) { // 自定义错误提示信息 die('连接错误'.$mysqli->connect_errno.': '. $mysqli->connect_error); } echo '<h1>连接成功</h1>'; //设置客端默认的字符编码集 //创建数据库连接方法一:将连接参数转为数组 $mysqli->set_charset($db['charset']); //创建数据库连接方法二:将连接参数转为参数 //$mysqli->set_charset($db_charset); // 将默认数据库在连接的时候,直接通过构造方法传入
点击 "运行实例" 按钮查看在线实例
5 手写作业