批改状态:合格
老师批语:
问答: 什么类,什么是对象,举例说明:
类的概念:类是具有相同属性和服务的一组对象的集合。它为属于该类的所有对象提供了统一的抽象描述,其内部包括属性和服务两个主要部分。在面向对象的编程语言中,类是一个独立的程序单位,它应该有一个类名并包括属性说明和服务说明两个主要部分。
对象的概念:对象是系统中用来描述客观事物的一个实体,它是构成系统的一个基本单位。一个对象由一组属性和对这组属性进行操作的一组服务组成。
举例说明:类就是爸爸,对象就是儿子,一个模子刻出来一样的
参考object/demo3.php,自定义类与实例化,要求必须将属性私有化,通过公共接口__set()和get()进行访问
<?php
class Boyfriend1
{
private $name;
private $age;
private $stature = [];
private $data = [];
public function __construct($name,$age,array $stature)
{
$this->name = $name;
$this->age = $age;
$this->stature = $stature;
}
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/boyfriend1.php';
$Boyfriend1 = new boyfriend1('比利·海灵顿','110kg','180','50,32,24');
echo $Boyfriend1->name,'<br>';
echo $Boyfriend1->weight,'<br>';
echo $Boyfriend1->high,'<br>';
echo $Boyfriend1->stature,'<br>';点击 "运行实例" 按钮查看在线实例
MySQL常用的增删改查语句(CURD)
选择:
select * from table1 where 范围
插入:
insert into table1(field1,field2) values(value1,value2)
删除:
delete from table1 where 范围
更新:
update table1 set field1=value1 where 范围
数据库的连接与检测
class Db{
//连接参数与返回值
public $db = null;
public $host;
public $user;
public $pass;
//构造方法
public function __construct($host='127.0.0.1',$user='root',$pass='root')
{
$this->host=$host;
$this->user=$user;
$this->pass=$pass;
// 确保实例化对象的时候能自动连接上数据库
$this->connect();
}
//数据库连接
private function connect(){
$this->db=mysqli_connect($this->host,$this->user,$this->pass);
}点击 "运行实例" 按钮查看在线实例
与MySQLil连接相关的几个属性和方法的名称,参数,与功能和用法
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号