实例
1.类与对象: 类是描述 同类对象 的集合;对象是 类描述的实体。就是模具和铸件的关系 <br> <?php //定义一个类 class Worker{ public $name; public $job; public $age; function __construct($name='',$job='',$age="秘密"){ $this->name = $name; $this->job = $job; $this->age = $age; } } //实例化三个对象 $p1 = new Worker('小明','厨师',50); $p2 = new Worker('小红','会计',30); $p3 = new Worker('小芳','老师',24); echo $p1->name."今年 ".$p1->age." 岁,工作是 ".$p1->job." <br>"; echo $p2->name."今年 ".$p2->age." 岁,工作是 ".$p2->job." <br>"; echo $p3->name."今年 ".$p3->age." 岁,工作是 ".$p3->job." <br>"; ?> 2.自定义类和实例化,并用__set() __get() 魔术方法操作类的私有属性<br> <?php class Animal{ private $type; private $leg; private $color; private $name; function __set($k,$v){ if(empty($this->$k)){ $this->$k = $v; }else{ return "属性不存在"; } } function __get($k){ return $this->$k; } function show(){ echo "这只".$this->color."的".$this->type."名字叫".$this->name."有".$this->leg."条腿<br>"; } } $cat = new Animal(); $cat->type = "猫"; $cat->name = "小花"; $cat->leg = 4; $cat->color = "橙色"; $cat->show(); ?> 自定义类和实例化: <?php class Phone{ private $model; private $screen; private $price; function __construct($model='国产',$screen=4.7,$price=4000){ $this->model = $model; $this->screen = $screen; $this->price = $price; } function __set($name,$value){ if(isset($this->$name)){ $this->$name = $value; }else{ echo "<strong>无此属性</strong>"; } } function __get($name){ if (isset($this->$name)) { return $this->$name; }else{ echo null; } } } $nokia = new Phone("nokia"); echo "<br>手机: 牌子 ",$nokia->model," 尺寸 ",$nokia->screen," 价格 ",$nokia->price; $nokia->price = 54321; $nokia->screen = 6.0; echo "<br>手机: 牌子 ",$nokia->model," 尺寸 ",$nokia->screen," 价格 ",$nokia->price; $nokia->scrseen = 6.0; ?>
运行实例 »
点击 "运行实例" 按钮查看在线实例
二、数据库的基础操作:
实例
<?php $config = [ 'host'=>'localhost', 'username' =>'abc', 'password' =>'abc', 'dbname' =>'test', ]; require 'config.php';//引入配置文件 //链接数据库 @$conn = new mysqli($config['host'],$config['username'],$config['password']); //判断是否连接,若连接出错,则报错 !$conn->connect_error or die('数据库连接失败!'); if($conn->connect_error){ echo "数据库连接失败".$conn->connect_error; }else{ echo "数据库连接成功!<br>"; } //选择数据库,也可以在连接的时候在第4个参数中传入 $conn->select_db('test'); //设置传输字符编码 $conn->set_charset('utf8'); //插入一点数据 $sql = "insert into user (name,pwd) values ('小明','123'),('小红','311'),('小白','3555')"; $res = $conn->query($sql); if($res){ echo "先插入3条数据<br>"; } //组建查询sql语句 $sql = "select * from user"; //执行sql语句,返回结果集 $res = $conn->query($sql); //判断是否查询成功 if($res->num_rows > 0){ //以关联数组的形式遍历结果集 while ($arr = $res->fetch_assoc()) { echo "id:".$arr['id']."=>用户名".$arr['name']."密码".$arr['pwd']."<br>"; } }else{ echo "查询失败"; } //删除名字是小明的数据 $sql = "delete from user where name='小明'"; $res = $conn->query($sql); //判断是否删除成功 if($res){ echo "删除成功"; }else{ echo "删除失败"; } //再次查询数据库中的表 $sql = "select * from user"; $res =$conn->query($sql); echo "数据库里共有".$res->num_rows."条数据"; if($res->num_rows!=0){ while($arr = $res->fetch_assoc()){ echo "id:".$arr['id']."=>用户名".$arr['name']."密码".$arr['pwd']."<br>"; } } //清空数据表,一条一条删除 $sql="delete from user"; //清空表 $sql="truncate table user"; if($conn->query($sql)){ echo "数据库清空了!!!!!<br>"; } //插入一条数据 name=>小智 pwd=>181818 $sql = "insert into user (name,pwd) values ('小智','181818')"; if($conn->query($sql)){ echo "成功插入一条小明的数据<br>"; }else{ echo "插入失败<br>"; } //查看一下是否真的有这条数据 $sql="select * from user where name='小智'"; $res =$conn->query($sql); $arr = $res->fetch_assoc(); echo "现在数据库中有了一条数据:姓名是{$arr['name']},密码是{$arr['pwd']}"; //修改小智的密码变成 999888 $sql = "update user set pwd =999888 where name='小智'"; if($conn->query($sql)){ echo "<br>修改成功<br>"; } //再查看小智的密码是多少 $sql = "select pwd from user where name='小智'"; $res = $conn->query($sql); echo "现在小明的密码变成了".$res->fetch_assoc()['pwd']; ?>
运行实例 »
点击 "运行实例" 按钮查看在线实例