php面向对象对象概念理解
简单的理解
形象“好比两个人打架,你要用程序去描述它,面向结构了,你是把每一步的对打都写好了,比如对方出什么招数,你出什么招数,必须每一步都写出来,面向对象的方法了,你要把打架的过程拆分成几个部分,打架前,打架中,及其之后”。
现在对于PHP的面向对象的做法,已经学习了一段时间,做的系统里面也有是用面向对象的方法做的了。
说一说面向对象的好处吧,它可以让系统很好的模块化,可以让很多的程序员一起工作,提高了编码的效率。对于整个系统的维护和更新也方便了很多的。
下面帖个类出来,大家感受一下吧。这个是一个数据库链接和操作的基础类,它为其他类说引用吧。
/*
数据库类文件:class_database.php
数据库操作类,本类是其他类操作的基础,即其他类函数的实现一般情况下通过数据库类实现
创建世间:2007年5月17日
*/
include_once("config.inc"); //包含系统配置文件
class data_class
{
//属性
private $host; //服务器名
private $user; //用户名
private $pwd; //密码
private $name; //数据库名
private $connection; //连接标识
//方法
//__get():获取属性值
function __get($property_name){
if(isset($this->$property_name))
{
return($this->$property_name);
} else {
return(NULL);
}
}
//__set():设置单个私有数据属性值,用于少量的修改数据
function __set($property_name, $value)
{
$this->$property_name = $value;
}
//__construct:构造函数,建立连接,在函数建立时自动调用建立,原则新建对象时不显式调用
function __construct()
{
$this->host=sys_conf::$dbhost; //使用sys_conf类的静态属性
$this->user=sys_conf::$dbuser;
$this->pwd=sys_conf::$dbpswd;
$this->name=sys_conf::$dbname;
//建立与数据库的连接
$this->connection=mysql_connect ($this->host,$this->user,$this->pwd);//建立连接
mysql_query("set names utf8");//字符集的统一
mysql_select_db("$this->name", $this->connection); //选择数据库挑战杯
}
//__destruct:析构函数,断开连接,在函数执行完毕时自动调用析构。实现关闭数据库的连接,保证数据
库数据的安全
function __destruct()
{
mysql_close($this->connection);
}
//增删改:参数$sql为Insert update
function execute($sql)
{
mysql_query($sql);
//echo "写入数据库成功了";
//echo "我是dataclass类的execute函数";
}//execute
//查:参数$sql为Insert语句
//返回值为对象数组,数组中的每一元素为一行记录构成的对象
function query($sql)
{
$result_array=array(); //返回数组
$i=0; //数组下标
$query_result=@mysql_query($sql,$this->connection); //查询数据
while($row=@mysql_fetch_object($query_result))
{
$result_array[$i ]=$row;
}//while
return $result_array;
}
//获得查询结果的纪录数函数
function result_query($sql)
{
$result=mysql_query($sql);
$result_c=mysql_num_rows($result);
return $result_c;
}
}
?>

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment