php学习笔记之面向对象编程
前端之家收集整理的这篇文章主要介绍了
php学习笔记之面向对象编程,
前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
@H_403_0@<div class="codetitle"><a style="CURSOR: pointer" data="16946" class="copybut" id="copybut16946" onclick="doCopy('code16946')"> 代码如下:
<div class="codebody" id="code16946">
<?
PHPclass db {
private $
MysqLi; //
数据库连接
private $options; //
sql选项
private $tableName; //表名
public function __construct($tabName) {
$this->tableName = $tabName;
$this->db ();
}
private function db() {
$this->
MysqLi = new
MysqLi ( 'localhost','root','','hdcms' );
$this->
MysqLi->query("SET NAMES GBK");
}
public function fields($fildsArr) {
if (empty ( $fildsArr )) {
$this->options ['fields'] = '';
}
if (is_array ( $fildsArr )) {
$this->options ['fields'] = implode ( ',',$fildsArr );
} else {
$this->options ['fields'] = $fildsArr;
}
return $this;
}
public function order($str) {
$this->options ['order'] = "ORDER BY " . $str;
return $this;
}
public function select() {
$
sql = "SELECT {$this->options['fields']} FROM {$this->tableName} {$this->options['order']}";
return $this->query ( $
sql );
}
private function query($
sql) {
$result = $this->
MysqLi
->query ( $
sql );
$rows = array ();
while ( $row = $result->fetch_assoc () ) {
$rows [] = $row;
}
return $rows;
}
private function close() {
$this->
MysqLi
->close ();
}
function __destruct() {
$this->close ();
}
}
$chanel = new db ( "hdw_channel" );
$chanelInfo = $chanel->fields ( 'id,cname,cpath' )
->select ();
echo "
";
printr ( $chanelInfo );
@H403_0@class a {
protected function aa(){
echo 222;
}
}
class b extends a{
function bb(){
$this->aa();
}
}
$c = new b();
$c->bb();