本文是一个php学习笔记为大家讲解的是php的面向对象编程,感兴趣的同学参考下。 <?php class db { private $mysqli; //数据库连接 private $options; //SQL选项 private $tableName; //表名 public function __construct($tabName) { $this->tableName = $tabName; $this->db (); } private function db() { &nb
本文是一个php学习笔记为大家讲解的是php的面向对象编程,感兴趣的同学参考下。
<?php
class 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 "<pre>";
print_r ( $chanelInfo );
class a {
protected function aa(){
echo 222;
}
}
class b extends a{
function bb(){
$this->aa();
}
}
$c = new b();
$c->bb();
public 公有的:本类,子类,外部对象都可以调用
protected 受保护的:本类 子类,可以执行,外部对象不可以调用
private 私有的:只能本类执行,子类与外部对象都不可调用
声明:本文内容来源自网络,文字、图片等素材版权属于原作者,平台转载素材出于传递更多信息,文章内容仅供参考与学习,切勿作为商业目的使用。如果侵害了您的合法权益,请您及时与我们联系,我们会在第一时间进行处理!我们尊重版权,也致力于保护版权,站搜网感谢您的分享!