登录  /  注册
博主信息
博文 37
粉丝 0
评论 0
访问量 31126
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
类和对象,类的构造器,魔术方法__get()和__set(),mysql连接数据库的方式--2018年8月30日11时38分
新竹网络_Terry的博客
原创
713人浏览过

这一节课主要讲的是类和数据库的知识,主要是类和对象,类的构造器,魔术方法__get()和__set(),mysql连接数据库1、

什么是类 ?什么是对象?举例说明

答:类是对象的模板,对象是类的一个实例。 举例:类:class Student{} 对象:$student=new Student()

代码


实例

<?php
class Student{//定义一个Student类
    //访问控制:私有化pricate
    private $name;
    //年龄
    private $age;
    //成绩分数
    private $score=[];
    //属性收集器
    private $data=[];
    //声明构造方法:对象属性的初始化,在类实例化时自动调用
    public function __construct($name,$age,array $score)
    {//private访问符限制的属性仅在当前对象内部可以使用
        $this->name = $name;
        $this->age = $age;
        $this->score = $score;
    }
    //创建对外访问的公共接口
    //类中用双下划线的方法是系统定义的,由系统自动调用是魔术方法
    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;
    }
    //设置器
    public function __set($name, $value)
    {
       $this->name = $value;
    }

}
?>

运行实例 »

点击 "运行实例" 按钮查看在线实例

实例

<?php
//插入外部的类文件
require 'class/Student.php';
//类的实例化
$student1 = new Student('杨过',26,[80,90,86]);
echo $student1->name,'<br>';
echo $student1->age,'<br>';
print_r($student1->score) ;

运行实例 »

点击 "运行实例" 按钮查看在线实例

预览图

RU`G1R2C{2PEXDRKO_QS~5P.png

代码


实例

//在表staff中插入数据
insert into `staff` (`name`,`salary`) values (`aaa`,4544);
insert into `staff` (`name`,`salary`) values (`aaa`,4544);
insert into `staff` (`name`,`salary`) values (`aaa`,4544);
insert into `staff` (`name`,`salary`) values (`aaa`,4544);
insert into `staff` (`name`,`salary`) values (`aaa`,4544);
insert into `staff` (`name`,`salary`) values (`aaa`,4544);
insert into `staff` (`name`,`salary`) values (`aaa`,4544);
insert into `staff` (`name`,`salary`) values (`aaa`,4544);
insert into `staff` (`name`,`salary`) values (`aaa`,4544);
insert into `staff` (`name`,`salary`) values (`aaa`,4544);
//更新staff表中id=17的age和salary字段
update `staff` set `age`=30,`salary`=1000 where `id`=17;
update `staff` set `age`=30,`salary`=1000 where `id`=17;
update `staff` set `age`=30,`salary`=1000 where `id`=17;
update `staff` set `age`=30,`salary`=1000 where `id`=17;
update `staff` set `age`=30,`salary`=1000 where `id`=17;
update `staff` set `age`=30,`salary`=1000 where `id`=17;
update `staff` set `age`=30,`salary`=1000 where `id`=17;
update `staff` set `age`=30,`salary`=1000 where `id`=17;
update `staff` set `age`=30,`salary`=1000 where `id`=17;
update `staff` set `age`=30,`salary`=1000 where `id`=17;
//查询staff表
select * from `staff` where 条件;
select * from `staff` where 条件;
select * from `staff` where 条件;
select * from `staff` where 条件;
select * from `staff` where 条件;
select * from `staff` where 条件;
select * from `staff` where 条件;
select * from `staff` where 条件;
select * from `staff` where 条件;
select * from `staff` where 条件;
//删除
delete * from `staff` where 条件;
delete * from `staff` where 条件;
delete * from `staff` where 条件;
delete * from `staff` where 条件;
delete * from `staff` where 条件;
delete * from `staff` where 条件;
delete * from `staff` where 条件;
delete * from `staff` where 条件;
delete * from `staff` where 条件;
delete * from `staff` where 条件;

运行实例 »

点击 "运行实例" 按钮查看在线实例

代码


实例

<?php
$db = [
    'host' => '127.0.0.1',
    'user' => 'root',
    'pass' => '5201314',
    'name' => 'php',
    'charset' => 'utf8',
];

运行实例 »

点击 "运行实例" 按钮查看在线实例

实例

<?php
//插入配置文件
require 'config.php';
//创建一个数据库连接,并返回mysqli对象
$mysqli= new mysqli($db['host'],$db['user'],$db['pass'], $db['name']);
//判断是否连接成功
if($mysqli->connect_errno){
    //自定义错误提示信息
    die('连接错误'.$mysqli->connect_errno.':'.$mysqli->connect_error);
}
echo '<h1>连接成功</h1>';
//设置客 户端默认的字符编码集
$mysqli->set_charset($db['charset']);

运行实例 »

点击 "运行实例" 按钮查看在线实例

预览图

BWVJ9N)TG~ZXE9C8MW[59C0.png

手写

P53P(6]EXPT)5($~076ZYVW.png

对于数据库连接用到的属性和方法记得更加清楚,也知道怎样去写数据库连接

总结

1、知道什么是类什么是对象,类是对象的模板,对象是类的一个实例

2、怎么写公共接口__set()和get(),同时知道了怎么进行访问

3、熟悉了MySQL常用的增删改查语句

4、msql的连接所用到的属性和方法以及adminer.php的使用

批改状态:合格

老师批语:
本博文版权归博主所有,转载请注明地址!如有侵权、违法,请联系admin@php.cn举报处理!
全部评论 文明上网理性发言,请遵守新闻评论服务协议
0条评论
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2024 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号

  • 登录PHP中文网,和优秀的人一起学习!
    全站2000+教程免费学