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

实例

<?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>';
}

运行实例 »

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


Correction status:Uncorrected

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