博主信息
博文 24
粉丝 0
评论 0
访问量 20642
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
对象基础与MySQLi对象编程-2018年8月30日
鱼越龙门的博客
原创
796人浏览过

1问答: 什么类,什么是对象,举例说明

类就是对象的模板,对象是类的实例,例如女明星就是一个类,凤姐,冰冰姐这些就是对象。

2代码:

实例

<?php
class Person{
    private $name;//私有属性姓名
    private $age;//私有属性年龄
    private $sex;//私有属性姓别
    public  function  __construct($name,$age,$sex)//构造方法
    {
        $this->name=$name;
        $this->age=$age;
        $this->sex=$sex;
    }
    private $data=[];//属性收集器
    //创建对外访问的公共接口
    // 类中用双下划线的方法是系统定义,由系统自动调用,叫魔术方法
    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){
        return $this->$name=$value;
    }
}

运行实例 »

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

实例

<?php
require './demo1.php';
$p=new Person('tom',18,'man');
echo $p->name;
$p->age=20;
echo $p->age;
echo $p->vv;

运行实例 »

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

实例

SELECT * FROM `staff` //查询
SELECT `name`,`age` FROM `staff` 
SELECT 30+50 as Result
SELECT * FROM `staff` 
SELECT `name`,`age` FROM `staff` 
SELECT 30+50 as Result
SELECT * FROM `staff` 
SELECT `name`,`age` FROM `staff` 
SELECT 30+50 as Result
SELECT * FROM `staff` 
SELECT `name`,`age` FROM `staff` 
SELECT 30+50 as Result

INSERT INTO `staff`( `name`,`age` ) VALUES('tom',20);//增加
INSERT INTO `staff`( `name`,`age` ) VALUES('tom',20);//增加
INSERT INTO `staff`( `name`,`age` ) VALUES('tom',20);//增加
INSERT INTO `staff`( `name`,`age` ) VALUES('tom',20);//增加
INSERT INTO `staff`( `name`,`age` ) VALUES('tom',20);//增加
INSERT INTO `staff`( `name`,`age` ) VALUES('tom',20);//增加
INSERT INTO `staff`( `name`,`age` ) VALUES('tom',20);//增加
INSERT INTO `staff`( `name`,`age` ) VALUES('tom',20);//增加
INSERT INTO `staff`( `name`,`age` ) VALUES('tom',20);//增加
INSERT INTO `staff`( `name`,`age` ) VALUES('tom',20);//增加

delete from `staff` where `staff_id`=12; //删除
delete from `staff` where `staff_id`=12; 
delete from `staff` where `staff_id`=12; 
delete from `staff` where `staff_id`=12;
delete from `staff` where `staff_id`=12; 
delete from `staff` where `staff_id`=12; 
delete from `staff` where `staff_id`=12; 
delete from `staff` where `staff_id`=12; 
delete from `staff` where `staff_id`=12; 
delete from `staff` where `staff_id`=12; 

update `staff` set `name`='lilei' where `staff_id`=17; //修改
update `staff` set `name`='lilei' where `staff_id`=17; //修改
update `staff` set `name`='lilei' where `staff_id`=17; //修改
update `staff` set `name`='lilei' where `staff_id`=17; //修改
update `staff` set `name`='lilei' where `staff_id`=17; //修改
update `staff` set `name`='lilei' where `staff_id`=17; //修改
update `staff` set `name`='lilei' where `staff_id`=17; //修改
update `staff` set `name`='lilei' where `staff_id`=17; //修改
update `staff` set `name`='lilei' where `staff_id`=17; //修改
update `staff` set `name`='lilei' where `staff_id`=17; //修改
update `staff` set `name`='lilei' where `staff_id`=17; //修改

运行实例 »

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

实例

<?php
$db=[
    'host'=>'127.0.0.1', //主机名
    'user'=>'root',  //用户名
    'pwd'=>'root',  //密码
    'name'=>'php',  //数据库
    'charset'=>'utf8'  //字符集
];

运行实例 »

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

实例

<?php
require 'config.php';
error_reporting(E_ALL ^E_WARNING);
$mysqli=new mysqli($db['host'],$db['user'],$db['pwd'],$db['name']);
//var_dump($myqli);
//判断是否连接成功?
if($mysqli->connect_errno){
    // 自定义错误提示信息
    die('错误信息:'.$mysqli->connect_errno.':'.$mysqli->connect_error);
}
echo '<h1>连接成功</h1>';
//设置kehu端默认的字符编码集
$mysqli->set_charset($db['charset']);

运行实例 »

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

QQ图片20180830163249.jpg

批改状态:合格

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

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

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