Blogger Information
Blog 33
fans 0
comment 2
visits 37336
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
类和对象的关系,实例化对象,mysqli的链接,以及简单sql语句2018/8/29
cxw的博客
Original
1350 people have browsed it

通过今天的学习,我明白了类和对象之间的关系,并且创建类,实例化对象,并且通过mysqli链接数据库,并且掌握其基本的属性,以下是我的代码:

1,自定义类和实例化

实例

<?php
/**
 * Created by PhpStorm.
 * User: admin
 * Date: 2018/8/29
 * Time: 22:14
 */

class demo1
{
    //定义私有变量
    private $name;
    private  $age;
    private  $hobby;
    private  $score=[];

    //数据采集器
    private  $data=[];
    public function  __construct($name='小明',$age=15,$hobby='篮球',array $score=['80','45','25'])
    {
        $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='15')
//{
//    $this->age=$age;
//}
//    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];
    }
}

运行实例 »

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

引用类:

实例

<meta charset="UTF-8">
<?php

require 'demo1.php';
$result=new demo1();

//$result->setName('小红');
//$result->setAge('20');
//$result->setHobby('篮球');
//$result->setScore([120,130,180]);

//简化后的赋值方法
$result->age='66';
$result->name='小强';
$result->hobby='兵乓球';
$result->score=[100,120,130];

echo  $result->show();

运行实例 »

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

1,使用mysqli链接数据库,和其简单的增删改查语句

实例

<meta charset="UTF-8">
<?php
/**
 * 数据库链接
 *
 */

$db=[
    'db_host'=>'127.0.0.1',
    'db_user'=>'root',
    'db_pass'=>'root',
    'db_name'=>'test',
    'db_chaset'=>'utf8'
];
$name='小红';
//$mysql->set_charset($db_charset);
$mysql=new mysqli($db['db_host'],$db['db_user'],$db['db_pass'],$db['db_name']);
if ($mysql->connect_errno)
{
    die('链接失败'.$mysql->connect_errno.":".$mysql->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>';


}

运行实例 »

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

手抄图片:

IMG_20180830_151201.jpg

总结:

1,类是对象的抽象,对象是类的具体实例,对象必须通过new 关键字进行实例化,才能调用其属性和方法

2,mysqli-error 返回错误信息, $mysqli->errno 返回错误代码 $mysqli->select_db()默认查询的数据库 $mysqli->set_charset()设置编码,

Correction status:qualified

Teacher's comments:
Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post