獲取指定日期的時間戳
strtotime('2017-2-20');
time();獲取當(dāng)前時間戳
類中使用數(shù)據(jù)庫
<?php
/**
* Created by PhpStorm.
* User: Administrator
* Date: 2017/2/28
* Time: 10:33
* __call($name,$arguments)---當(dāng)對象調(diào)用不存在的方法或者無權(quán)限訪問時,自動調(diào)用此方法,
* $name --方法名,$arguments--接收調(diào)用方法時傳遞的參數(shù);
*
*/
//$mysql = new Mysql('localhost','root','123456');
//$mysql->usedb('cms');
//$results = $mysql->selectAll('select * from users'); //返回一個二維數(shù)組,存的是記錄
//
//array(
// array('account'=>'lxh','age'=>28),
// array('account'=>'lx1h','age'=>28),
// array('account'=>'lx11h','age'=>28)
//);
//
//$lists = $mysql->top2(2); //從第三條記錄起查詢兩條記錄,參數(shù)是起始值 方法名構(gòu)成的規(guī)則是top+num
//$lists = $mysql->top2(); //從第一條記錄起查詢兩條記錄,參數(shù)是起始值
class Mysql
{
private $link;
private $result;
private $sql='';
public function __construct($host,$user,$psw)
{
$this->link=@mysqli_connect($host,$user,$psw) or die('連接數(shù)據(jù)服務(wù)器失敗');
}
public function usedb($name){
mysqli_query($this->link,'use '.$name) or die('連接數(shù)據(jù)庫失敗');
}
public function selectAll($sql){
if(!empty($sql)) $this->sql=$sql;
$result=mysqli_query($this->link,$this->sql);
if(!$result){
trigger_error($this->sql.'sql語句有誤;',E_USER_ERROR);
}
if(is_object($result)){
$this->result=$result;
}
$list = [];
while($row=mysqli_fetch_assoc($this->result)){
$list[] = $row;
}
return $list;
}
public function __call($name,$arguments){
if(stripos($name,'top')==0){//判斷是否是top函數(shù);
$start=isset($arguments)?intval($arguments[0]):0;//設(shè)置參數(shù)默認(rèn)值;
return $this->top($start,substr($name,3));//調(diào)用top函數(shù)
}
}
private function top($start,$num=0){
$this->sql='select id,title from cms_photo limit '.$start.','.$num;
return $this->selectAll($this->sql);
}
//用于查詢單條記錄sql語句
public function fetch($sql){
$this->result=mysqli_query($this->link,$sql);
return mysqli_fetch_assoc($this->result);
}
public function __destruct()
{
$this->free_result();//釋放資源,
$this->close();//關(guān)閉連接
}
public function close(){
if(!empty($this->link)){
mysqli_close($this->link);
$this->link = null;
}
}
public function free_result(){
if($this->result){
mysqli_free_result($this->result);
}
}
}
$mysql= new Mysql('localhost','root','root');
$mysql->usedb('mytest');
$result=$mysql->selectAll('select id,title from cms_photo');
//var_dump($result);
$lists = $mysql->top2(1);
var_dump($lists);
$list=$mysql->fetch('select id,title from cms_photo where id=3');
print_r($list);
__call($name,$arguments)
__call($name,$arguments)---當(dāng)對象調(diào)用不存在的方法或者無權(quán)限訪問的方法時自動調(diào)用該方法,$name---方法名,$arguments接收的是方法傳遞的參數(shù),是數(shù)組
__callStaric($name,$arguments)當(dāng)調(diào)用不存在的靜態(tài)方法時調(diào)用
class Page{
private $pageSize;
private $size;
public $config = [
'total'=>0,
'row'=>8
];
public function __construct($total,$limit)
{
$this->config['total'] = $total;
$this->config['row'] = $limit;
$this->getPage();
}
public function __set($name, $value)
{
if(!property_exists(__CLASS__,$name)){
$this->config[$name] = $value;
switch($name){
case 'row':
case 'total':
$this->getPage();
break;
}
}
}
public function __get($name)
{
if(isset($this->config[$name])) return $this->config[$name];
if(property_exists(__CLASS__,$name)) return $this->$name;
}
/*public function show(){
$str = '';
for($i=1;$i<=$this->pageSize;$i++){
$str.='<a href="">'.$i.'</a>';
}
return $str;
}*/
private function getPage(){
$this->pageSize = ceil($this->config['total']/$this->config['row']);
}
public function __call($name, $arguments)
{
if(stripos($name,'show')===0){
//分頁輸出
$type = strtolower(substr($name,4));
switch($type){
case 'refresh':
return $this->refreshShow();
break;
case 'ajax':
return $this->ajaxShow();
break;
}
}
}
private function refreshShow(){
$str = '';
for($i=1;$i<=$this->pageSize;$i++){
$str.='<a href="?p='.$i.'">'.$i.'</a>';
}
return $str;
}
private function ajaxShow(){
$str = '';
for($i=1;$i<=$this->pageSize;$i++){
$str.='<a href="javascript:void(0)" page="'.$i.'">'.$i.'</a>';
}
return $str;
}
}
$page = new Page(60,10);
echo $page->showRefresh().'<hr />'; //<a href="?p=1">1</a><a href="">2</a>
echo $page->showAjax().'<hr />'; //<a href="javascript:void(0)" url="?p=1">1</a><a href="javascript:void(0)" url="?p=2">2</a>
echo '<hr />';
成員方法,靜態(tài)方法;
new創(chuàng)建的對象,調(diào)用函數(shù)的方法是成員方法,
對象去調(diào)用的是方法是:成員方法,date=new Date(); date.getMonth()
類名調(diào)用的方法是靜態(tài)方法;$.ajax
靜態(tài)屬性,靜態(tài)方法
調(diào)用方法:
類名::方法名([實(shí)參]) 類名::屬性名--兩個冒號,屬性名要有$,如果在類中使用,將類名用self代替 eg:Mysql::connect(), Mysql::$link
在靜態(tài)方法中不能在用$this
**定義常量const 常量名=值;常量名大寫,定義后不能更改** **訪問:類名::常量名**
const NAME="aaa"`
訪問:類名::常量名
class name{
常量
靜態(tài)屬性
成員屬性
靜態(tài)方法
成員方法
}
class Shape{
private $width;
private $height;
public static $total;//設(shè)置靜態(tài)屬性后,可以在類外賣設(shè)置初始值;
//靜態(tài)屬性;
public function __construct($w,$h)
{
self::isInt($w);//調(diào)用本類的方法
self::isInt($h);
$this->width=$w;
$this->height=$h;
self::$total++;//設(shè)置靜態(tài)屬性;
}
public static function getTotal(){
return self::$total;//通過訪問這個方法來獲取$total的值;
}
public function getLen(){
return ($this->height+$this->width)*2;
}
public static function isInt($val){
if(!is_int($val))trigger_error($val.'不是整數(shù)',E_USER_ERROR);
}
}
Shape::$total=0;//靜態(tài)屬性可以在類外賣設(shè)置初始值;
$s1=new Shape(34,20);
$s2=new Shape(34,20);
$s3=new Shape(60,20);
echo $s3->getLen().'<br/>';
echo Shape::getTotal();
繼承
可以共享數(shù)據(jù)(子類將繼承父類的非私有屬性,方法,常量等)被繼承的類稱為父類或著超類,繼承出來的為子類;
PHP中只支持單一繼承;一個類只能有一個父類
調(diào)用方式:parent::方法([參數(shù)])
class Student extends Person{};Person 為父類,Student 為子類
調(diào)用父類的構(gòu)造方法
parent::__construct([參數(shù)]);
當(dāng)子類方法名與父類一樣是稱為重寫,以子類方法實(shí)現(xiàn)功能;
保留父類功能:parent::方法名