㈠ 什麼是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(){
}
……
}
不過不如其他面向對象語言那樣嚴謹和規范。只能說勉強有這個功能罷了。
不過我感覺有點多此一舉……