导航:首页 > 编程语言 > php封装对象

php封装对象

发布时间:2022-05-08 06:42:32

㈠ 什么是php中的封装

封装是php面向对象的其中一个特性,将多个可重复使用的函数封装到一个类里面。在使用时直接实例化该类的某一个方法,获得需要的数据

如果是私有的方法和属性值,外部无法访问,具有一定的保护作用。

面向对象之封装 例子
class A{
public $name = '老王';
// protected $name = '老王';
//private $name = '老王';

//自己访问
public function saya(){
return $this->name;
}
}
//实例化对象
$b = new A;

//public:外部、家族、自己都可以访问
//protected:家族和自己都可以访问,外部无法访问
//private:自己可以访问,外部和家族都无法访问
echo '外部访问:'.$b->name.'<br>'; //如果是私有的,访问不了
echo '家族访问:'.$b->sayb().'<br>';
echo '自己访问:'.$b->saya().'<br>';

㈡ php 封装是什么啊

这个就是语法格式
$this 就代表着当前的类
如果想用类的属性就 $this->name 这么写

㈢ 在PHP开发中你习惯将一个事物封装为一个对象,还是简简单单就使用数组为什么

这两个东西不冲突。

说明你对概念的理解有问题,对象你应该说的是类,之所以用类是为了封装一些相对独立的功能,符合工程的一些做法,为什么这么做,网上很多关于面向对象方法的理解,可以参考一下。

数组就一种线性的存储方式而已,和对象不沾边,两者是不冲突的

㈣ 求PHP数据库封装类操作代码

<?php
class MySQL{
private $host; //服务器地址
private $name; //登录账号
private $pwd; //登录密码
private $dBase; //数据库名称
private $conn; //数据库链接资源
private $result; //结果集
private $msg; //返回结果
private $fields; //返回字段
private $fieldsNum; //返回字段数
private $rowsNum; //返回结果数
private $rowsRst; //返回单条记录的字段数组
private $filesArray = array(); //返回字段数组
private $rowsArray = array(); //返回结果数组
private $charset='utf8'; //设置操作的字符集
private $query_count=0; //查询结果次数
static private $_instance; //存储对象
//初始化类
private function __construct($host='',$name='',$pwd='',$dBase=''){
if($host != '') $this->host = $host;
if($name != '') $this->name = $name;
if($pwd != '') $this->pwd = $pwd;
if($dBase != '') $this->dBase = $dBase;
$this->init_conn();
}
//防止被克隆
private function __clone(){}
public static function getInstance($host='',$name='',$pwd='',$dBase=''){
if(FALSE == (self::$_instance instanceof self)){
self::$_instance = new self($host,$name,$pwd,$dBase);
}
return self::$_instance;
}
public function __set($name,$value){
$this->$name=$value;
}
public function __get($name){
return $this->$name;
}
//链接数据库
function init_conn(){
$this->conn=@mysql_connect($this->host,$this->name,$this->pwd) or die('connect db fail !');
@mysql_select_db($this->dBase,$this->conn) or die('select db fail !');
mysql_query("set names ".$this->charset);
}
//查询结果
function mysql_query_rst($sql){
if($this->conn == '') $this->init_conn();
$this->result = @mysql_query($sql,$this->conn);
$this->query_count++;
}
//取得字段数
function getFieldsNum($sql){
$this->mysql_query_rst($sql);
$this->fieldsNum = @mysql_num_fields($this->result);
}
//取得查询结果数
function getRowsNum($sql){
$this->mysql_query_rst($sql);
if(mysql_errno() == 0){
return @mysql_num_rows($this->result);
}else{
return '';
}
}
//取得记录数组(单条记录)
function getRowsRst($sql,$type=MYSQL_BOTH){
$this->mysql_query_rst($sql);
if(empty($this->result)) return '';
if(mysql_error() == 0){
$this->rowsRst = mysql_fetch_array($this->result,$type);
return $this->rowsRst;
}else{
return '';
}
}
//取得记录数组(多条记录)
function getRowsArray($sql,$type=MYSQL_BOTH){
!empty($this->rowsArray) ? $this->rowsArray=array() : '';
$this->mysql_query_rst($sql);
if(mysql_errno() == 0){
while($row = mysql_fetch_array($this->result,$type)) {
$this->rowsArray[] = $row;
}
return $this->rowsArray;
}else{
return '';
}
}
//更新、删除、添加记录数
function uidRst($sql){
if($this->conn == ''){
$this->init_conn();
}
@mysql_query($sql);
$this->rowsNum = @mysql_affected_rows();
if(mysql_errno() == 0){
return $this->rowsNum;
}else{
return '';
}
}
//返回最近插入的一条数据库的id值
function returnRstId($sql){
if($this->conn == ''){
$this->init_conn();
}
@mysql_query($sql);
if(mysql_errno() == 0){
return mysql_insert_id();
}else{
return '';
}
}
//获取对应的字段值
function getFields($sql,$fields){
$this->mysql_query_rst($sql);
if(mysql_errno() == 0){
if(mysql_num_rows($this->result) > 0){
$tmpfld = @mysql_fetch_row($this->result);
$this->fields = $tmpfld[$fields];

}
return $this->fields;
}else{
return '';
}
}
//错误信息
function msg_error(){
if(mysql_errno() != 0) {
$this->msg = mysql_error();
}
return $this->msg;
}
//释放结果集
function close_rst(){
mysql_free_result($this->result);
$this->msg = '';
$this->fieldsNum = 0;
$this->rowsNum = 0;
$this->filesArray = '';
$this->rowsArray = '';
}
//关闭数据库
function close_conn(){
$this->close_rst();
mysql_close($this->conn);
$this->conn = '';
}
//取得数据库版本
function db_version() {
return mysql_get_server_info();
}
}

㈤ 关于PHP封装类的一些问题

private $db_host是意为声明一个私有的量 db_host的意思是 数据库地址 例如 127.0.0.1 db_user 数据库管理员名称 一般为root

㈥ PHP如何封装方法

//下面定义一个方法,这个方法很简单,就是处理2个数的相加问题
functionadd($number1,$number2){
$sum=$number1+$number2;
echo$sum;
}
//我们来调用add()方法
add(1,2);
/**
说明:
function这个关键字就是声明方法的。在这个关键字后面的add就是方法的名称,括号中的是参数。
也可以没有参数的。大括号内是方法体。里面是该方法的逻辑。
下面的add(1,2)就是调用add这个方法。如果没有调用方法将不会被执行的。
*/
//定义一个带有返回值的方法
functionre($n1,$n2){
$sum=$n1+$n2;
return$sum;
}
//调用有返回值的方法,调用这个方法,值是5。
echore(2,3);


㈦ PHP真的能算是面向对象的语言吗

面向对象三大特性:封装,继承,多态
php可以封装对象,也可以继承,同样可以实现多态
多态是指指出重写和重载,php子类可以重写父类的方法,也可以通过方法的参数数量不同来重载方法,我觉得是面向对象语言

㈧ 关于php的面向对象问题和封装的问题!

比如,你在方法1中调用方法2,那么在执行到方法2的时候,会调用并执行方法2的程序,然后在执行方法2下面的方法1程序,类里的方法都是可以相互使用的!

㈨ php封装一个用户类,里面有登录注册方法,这个要怎么写

第一步:login.php

//登陆方法
public function login(){
//如果用户名和密码为空,则返回登陆页面
if(empty($_POST['username']) || empty($_POST['password'])){
$data['verifycode'] = rand(1000,9999);//生成一个四位数字的验证码
//将验证码放入session中,注意:参数是数组的格式
$this->session->set_userdata($data);
//注意:CI框架默认模板引擎解析的模板文件中变量不需要$符号
//$this->parser->parse("admin/login",$data);
//smarty模板变量赋值
$this->tp->assign("verifycode",$data['verifycode']);
//ci框架在模板文件中使用原生态的PHP语法输出数据
//$this->load->view('login',$data);//登陆页面,注意:参数2需要以数组的形式出现
//显示smarty模板引擎设定的模板文件
$this->tp->display("admin/login.php");
}else{
$username = isset($_POST['username'])&&!empty($_POST['username'])?trim($_POST['username']):'';//用户名
$password = isset($_POST['password'])&&!empty($_POST['password'])?trim($_POST['password']):'';//密码
$verifycode = isset($_POST['verifycode'])&&!empty($_POST['verifycode'])?trim($_POST['verifycode']):'';//验证码

//做验证码的校验
if($verifycode == $this->session->userdata('verifycode')){
//根据用户名及密码获取用户信息,注意:参数2是加密的密码
$user_info=$this->user_model->check_user_login($username,md5($password));
if($user_info['user_id'] > 0){
//将用户id、username、password放入cookie中
//第一种设置cookie的方式:采用php原生态的方法设置的cookie的值
//setcookie("user_id",$user_info['user_id'],86500);
//setcookie("username",$user_info['username'],86500);
//setcookie("password",$user_info['password'],86500);
//echo $_COOKIE['username'];

//第二种设置cookie的方式:通过CI框架的input类库
$this->input->set_cookie("username",$user_info['username'],3600);
$this->input->set_cookie("password",$user_info['password'],3600);
$this->input->set_cookie("user_id",$user_info['user_id'],3600);
//echo $this->input->cookie("password");//适用于控制器
//echo $this->input->cookie("username");//适用于控制器
//echo $_COOKIE['username'];//在模型类中可以通过这种方式获取cookie值
//echo $_COOKIE['password'];//在模型类中可以通过这种方式获取cookie值

//第三种设置cookie的方式:通过CI框架的cookie_helper.php函数库文件
//这种方式不是很灵验,建议大家采取第二种方式即可
//set_cookie("username",$user_info['username'],3600);
//echo get_cookie("username");

//session登陆时使用:将用户名和用户id存入session中
//$data['username']=$user_info['username'];
//$data['user_id']=$user_info['user_id'];
//$this->session->set_userdata($data);

//跳转到指定页面
//注意:site_url()与base_url()的区别,前者带index.php,后者不带index.php
header("location:".site_url("index/index"));
}
}else{
//跳转到登陆页面
header("location:".site_url("common/login"));
}
}
}
}

第二步:User_model.php

//cookie登陆:检测用户是否登陆,如果cookie值失效,则返回false,如果cookie值未失效,则根据cookie中的用户名和密码从数据库中获取用户信息,如果能获取到用户信息,则返回查询到的用户信息,如果没有查询到用户信息,则返回0
public function is_login(){
//获取cookie中的值
if(empty($_COOKIE['username']) || empty($_COOKIE['password'])){
$user_info = false;
}else{
$user_info=$this->check_user_login($_COOKIE['username'],$_COOKIE['password']);
}
return $user_info;
}

//根据用户名及加密密码从数据库中获取用户信息,如果能获取到,则返回获取到的用户信息,否则返回false,注意:密码为加密密码
public function check_user_login($username,$password){
//这里大家要注意:$password为md5加密后的密码
//$this->db->query("select * from ");
//快捷查询类的使用:能为我们提供快速获取数据的方法
//此数组为查询条件
//注意:关联数组
$arr=array(
'username'=>$username,//用户名
'password'=>$password,//加密密码
'status'=>1 //账户为开启状态
);
//在database.php文件中已经设置了数据表的前缀,所以此时数据表无需带前缀
$query = $this->db->get_where("users",$arr);
//返回二维数组
//$data=$query->result_array();
//返回一维数组
$user_info=$query->row_array();
if(!empty($user_info)){
return $user_info;
}else{
return false;
}
}

第三步:其它控制器:

public function __construct(){

//调用父类的构造函数
parent::__construct();
$this->load->library('tp'); //smarty模板解析类
$this->load->helper('url'); //url函数库文件
$this->load->model("user_model");//User_model模型类实例化对象
$this->cur_user=$this->user_model->is_login();
if($this->cur_user === false){
header("location:".site_url("common/login"));
}else{
//如果已经登陆,则重新设置cookie的有效期
$this->input->set_cookie("username",$this->cur_user['username'],3600);
$this->input->set_cookie("password",$this->cur_user['password'],3600);
$this->input->set_cookie("user_id",$this->cur_user['user_id'],3600);
}

$this->load->library('pagination');//分页类库
$this->load->model("role_model");//member_model模型类
$this->load->model("operation_model");//引用operation_model模型
$this->load->model("object_model");//引用object_model模型
$this->load->model("permission_model");//引用permission_model模型
}

㈩ php 对象封装问题

可以吧,通过implements 实现。
首先声明一个接口文件。
interface ClassName{
public function functionName($params1,$params2);
}
然后第二个文件
include_once('ClassName.php')
class ClassNameTwo implements ClassName{
function xxx(){
}

……

}
不过不如其他面向对象语言那样严谨和规范。只能说勉强有这个功能罢了。
不过我感觉有点多此一举……

阅读全文

与php封装对象相关的资料

热点内容
androidapp调用另一个app 浏览:621
数控铣床法兰克子程序编程 浏览:173
linux打包命令targz 浏览:996
抖音app是哪个 浏览:407
苹果app怎么上架 浏览:255
NA服务器地址 浏览:427
我的世界如何初始化服务器 浏览:97
哪个手机app天气预报最准 浏览:752
怎样把视频压缩至25m 浏览:570
vivox27文件夹怎么改变 浏览:727
新手玩狼人杀用什么app 浏览:615
pdf在线查看 浏览:954
安卓tv90如何关闭后台 浏览:683
php读取word乱码 浏览:755
minicom源码 浏览:1001
海尔冷柜压缩机 浏览:416
联通服务器如何调试信号 浏览:136
stata新命令 浏览:941
单调栈算法python 浏览:606
微信解压游戏怎么下载 浏览:962