面向对象
<?php
// 构造函数
class condb{
private $host;
private $username;
private $password;
private $charset;
private $dbname;
public function __construct($host,$username,$password,$dbname,$charset='utf-8'){
$this->host=$host;
$this->username=$username;
$this->password=$password;
$this->dbname=$dbname;
$this->charset=$charset;
}
public function getconnid(){
$connid=mysql_connect($this->host,$this->username,$this->password);
mysql_select_db($this->dbname,$connid);
mysql_query('set name'.$this->charset);
return $connid;
}
}
// 使用静态函数计算一组数的平均值
class stat{
public static function getavg($arrynum){
$totalnum=count($arrynum);
if($totalnum==0){
return null;
}else{
$num=0;
for($i=0;$i<$totalnum;$i++){
$num+=$arrynum[$i];
}return $num/$totalnum;
}
}
}
if(isset($_POST['nums'])&&trim($_POST['nums'])!=''){
$arr=explode(',', $_POST['nums']);
echo '该组数字的平均数为'.stat::getavg($arr);//使用静态方法不用new
}
// 重载实现不同数字类型的转换
class statparent{
public function execstat($val0,$val1){
return intval($val0+$val1);
}
}
class stats extends statparent{
public function execstat($val0,$val1){
return is_numeric($val0)&&is_numeric($val1)?intval($val0+$val1):$val0.$val1;//如果数字则计算数字;不是数字则连接两个参数
}
}
if(isset($_POST['txt1'])&&$_POST['txt1']!=''){
$stat=new stats();
echo '统计结果为'.$stat->execstat($_POST['txt1'],$_POST['txt2']);
}
// $this关键字调用汽车类型
class car{
private $colorflag;
private $typeflag;
public function __construct($colorflag,$typeflag){
$this->colorflag=$colorflag;
$this->typeflag=$typeflag;
}
public function getcolor(){
switch($this->colorflag){
case 0:
$color='黑色';
break;
case 1:
$color='白色';
}
return $color;
}
public function gettype(){
switch ($this->typeflag) {
case '0':
$type='奔驰';
break;
case '1':
$type='宝马';
break;
}
return $type;
}
public function getinfo(){
return '我的汽车是'.$this->getcolor().$this->gettype();
}
}
if(isset($_POST['color'])&&isset($_POST['type'])){
$colorflag=$_POST['color'];
$typeflag=$_POST['type'];
$car=new car($colorflag,$typeflag);
echo $car->getinfo();
}
// 静态属性和静态方法
class a{
private static $var;
private static function fun(){
}
public function main(){
self::$var;//应用静态属性
self::fun();//应用静态方法
// 在类内部使用self关键字加“::”应用类中的静态属性不要漏掉$
}
}
class bus{
public $color;
public function __construct($color){
$this->color=$color;
}
public function stop(){
return "汽车执行了刹车方法".'</br>';
}
}
$color='红色';
$car=new bus($color);
echo '汽车的颜色为--'.$car->color.'<br/>';
echo '行驶过程中--'.$car->stop();
// 使用const定义圆周率类常量
class circle{
const PI='3.14';
private $radius;
public function __construct($radius){
$this->radius=$radius;
}
public function getArea(){
return self::PI*pow($this->radius,2);
}
}
if(isset($_POST['radius'])&&trim($_POST['radius'])!=''){
$circle=new circle($_POST['radius']);
echo '园的面积是'.$circle->getArea();
}
// 苹果子类继承水果父类
class fruit{
private $color;
private $shape;
public function __construct($color,$shape){
$this->color=$color;
$this->shape=$shape;
}
public function getcolor(){
return $this->color;
}
public function getshape(){
return $this->shape;
}
}
class apple extends fruit{
public function __construct($color,$shape){
parent::__construct($color,$shape);
}
}
if(isset($_POST['color'])&&$_POST['color']!=''){
$apple=new apple($_POST['color'],$_POST['shape']);
echo '苹果是'.$apple->getcolor().','.$apple->getshape();
}
// 子类覆盖父类
class fruits{
public function getcolor(){
return '不同的水果颜色不同,无法确认';
}
}
final class apples extends fruits{
private $color;
public function __construct($color){
$this->color=$color;
}
public function getcolor(){
return '苹果颜色是'.$this->color;
}
}
$fruits=new fruits();
echo $fruits->getcolor().'</br>';
$apples=new apples('红色');
echo $apples->getcolor();
// 类多重接口的实现
interface name{
public function fun1();
private function fun2($param);
}
class myclass implements interface1,interface2,interface3{
// 需要实现各个接口中所声明的方法
}
// 通过继承实现多态
abstract class parent{
public function fun(){
return '父类fun()方法的实现';
}
}
class child1 extends parent{
public function fun(){
return 'child中fun()方法的实现';
}
}
class child2 extends parent{
public function fun(){
return 'child2中fun()方法的实现';
}
}
// 通过接口实现多态
interface A{
public function fun();
}
class class1 implements A{
public function fun(){
return '在类class1中实现接口A中的fun()方法';
}
}
class class2 implements A{
public function fun(){
return '在类class2中实现接口A中的fun';
}
}
// 抽象类
abstract class myclass{
// 类中属性和方法
}
// static修饰静态;直接通过类名调用类中静态方法不调用构造方法和析构方法
class test{
public function __construct(){
echo "执行构造方法<br/>";
}
static public function fun(){
echo "执行静态方法<br/>";
}
public function __destruct(){
echo "执行析构方法";
}
}
test::fun();//通过类名调用类中静态方法
// 克隆对象
$obj_new=clone $obj_old
// instanceof检测当前对象属于那个类;成功返回true失败返回false
对象名 instanceof 类名
下面是常常用的类
// 单例模式
class singletom{
private static $instance;
public static function getinstance(){
if(null==self::$instance){
self::$instance=new db();
}
return self::$instance;
}
private function __construct(){
}
private function __clone(){
}
}
$singletom=singletom::getinstance();
// 策略模式
abstract class strategy{
abstract public function fun();
}
class child1 extends strategy{
public function fun(){
// child1中fun()方法所实现的功能
}
}
class child2 extends strategy{
public function fun(){
// child2中fun()方法所实现的功能
}
}
if(条件1==true){
$obj=new child1();
}else{
$obj=new child2();
}
$obj->fun();
// 工厂模式
abstract class father{
}
class child1 extends father{
}
class child2 extends father{
}
class factory{
public static function create($condition){
if($condition=='条件1'){
return new child1();
}else($condition=='条件2'){
return new child2();
}
}
}

浙公网安备 33010602011771号