Correction status:Uncorrected
Teacher's comments:
<?php /** * 自定义类和实例化 */ class demo1{ //定义私有变量 private $name; private $age; private $hobby; private $score=[]; } //数据采集器 private $data=[]; public function __construst($name='小明',$age=15,$hobby='足球',array $score=['80','70','60']) { $this->name=$name; $this->age=$age; $this->hobby=$hobby; $this->score=$score; } //使用对象的获取器getter和setter(getter是获取值,而是setter是给其赋值) public function setName($name='王') { $this->name=$name; } public function getName() { return $this->name; } public function setAge($age=0) { if($age>=0 &&$age<=120) { $this->age=$age; } echo'非法数据'; } public function getAge() { return $this->age; } public function setHobby($Hobby='足球') { $this->hobby=$hobby; } public function getHobby() { return $this->hobby; } public function setScore($score=[80,70,60]) { $this->score=$score; } public function getScore() { return $this->score; } //简化写法__get类似get,__set类似set public function __get($name) { return $this->$name; } public function __set($name,$value) { $this->$name=$value; } //写一个函数将数据信息显示出来 function show(){ return '姓名:'.$this->name.'年龄:'.$this->age.'爱好:'.$this->hobby.'语数外成绩:'.$this->score[0].$this->score[1].$this->score[2]; }
点击 "运行实例" 按钮查看在线实例
<?php /** * 引用类 */ require 'demo1.php'; $result=new demo1(); $result->setName('小红'); $result->setAge('18'); $result->setHobby('足球'); $result->setScore('120,130.180'); //简化后赋值方法 $result->name='小红'; $result->age='66'; $result->hobby='兵乓球'; $result->score=[100,120,120]; echo $result->show();
点击 "运行实例" 按钮查看在线实例
<?php /** * 使用mysqli连接数据库,和其简单的增啥改查语句 */ $db=[ 'db_host'=>'127.0.0.1', 'db_user'=>'root', 'db_pass'=>'root', 'db_name'=>'test', 'db_charset'=>'utf8', ]; $name='小红'; $mysqli->set_charset($db_chartset); $mysqli=new mysqli($db['db_host_'],$db['db_user'],$db['db_pass'],$db['db_name']); if ($mysqli->connect_errno) { die('连接失败'.$mysqli->connect_errno.":".$mysqli->connect_errno); }else{ echo '<h1>连接成功</h1>'; //查询 //$result="select * from show"; // echo var_dump($result).$result.'<br>'; // $count="select COUNT(id) FROM show".'<br>'; // echo var_dump($count).$count; //增加 //$result="insert into show values ('小强',15)"; // echo var_dump($result).$result.'<br>'; // //更新 // $result="update show set name=".$name; // echo var_dump($result).$result.'<br>'; }
点击 "运行实例" 按钮查看在线实例