首页 技术 正文
技术 2022年11月15日
0 收藏 300 点赞 3,739 浏览 23184 个字

面向对象的基本概念,面向对象编程,oop,面向对象,面向对象的分析,面向对象的设计,面向对象的编程,什么是类。

类,属性和方法,类,对象,面向对象编程的三大特点。特点,封装性,继承性,多态性。

封装性,也可以称为信息隐藏。

继承性就是派生类(子类)自动继承一个或多个基类(父类)中的属性与方法,并可以重写或添加新的属性或方法。

多态性是指同一个类的不同对象,使用同一个方法可以获得不同的结果,这种技术称为多态性。

类的定义,成员方法,类的实例化,成员变量,类常量,构造方法和析构方法,继承和多态的实现,数据的隐藏,静态变量。

类的定义:

<?php
class SpoObject{//定义类
//…
}
?>

成员方法:

<?php
class SportObject{
function beatBasketball()($name,$height,$avoirdupois,$age,$sex){//声明成员方法
echo "姓名:".$name;//方法实现的功能}
}
?>

类的实例化:

对象名 -> 成员方法

成员变量

类中的变量,也称为成员变量

关键字 成员变量名

对象名 -> 成员变量

类常量

常量就是不会改变的量,是一个恒值。

定义常量使用关键字const

输出格式:

类名::常量名

构造方法和析构方法

void __construct([mixed args [,…]])
void __destruct ( void )

示例:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>成员变量</title>
<style type="text/css">
<!--
body,td,th {
font-size: 12px;
}
body {
margin-left: 10px;
margin-top: 10px;
margin-right: 10px;
margin-bottom: 10px;
}
-->
</style></head>
<body>
<?php
class SportObject{
public $name;//定义成员变量
public $height;//定义成员变量
public $avoirdupois;//定义成员变量public function bootFootBall($name,$height,$avoirdupois){//声明成员方法
$this->name=$name;
$this->height=$height;
$this->avoirdupois=$avoirdupois;
if($this->height<185 and $this->avoirdupois<85){
return $this->name.",符合踢足球的要求!";//方法实现的功能
}else{
return $this->name.",不符合踢足球的要求!";//方法实现的功能
}
}
}
$sport=new SportObject();//实例化类,并传递参数
echo $sport->bootFootBall('1','185','80');//执行类中的方法
?></body>
</html>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>成员方法</title>
<style type="text/css">
<!--
body,td,th {
font-size: 12px;
}
body {
margin-left: 10px;
margin-top: 10px;
margin-right: 10px;
margin-bottom: 10px;
}
-->
</style></head>
<body>
<?php
class SportObject{
function beatBasketball($name,$height,$avoirdupois,$age,$sex){//声明成员方法
if($height>180 and $avoirdupois<=100){
return $name.",符合打篮球的要求!";//方法实现的功能
}else{
return $name.",不符合打篮球的要求!";//方法实现的功能
}
}
}
$sport=new SportObject();
echo $sport->beatBasketball('1','185','80','20周岁','男');
?>
</body>
</html>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>常量</title>
<style type="text/css">
<!--
body,td,th {
font-size: 12px;
}
body {
margin-left: 10px;
margin-top: 10px;
margin-right: 10px;
margin-bottom: 10px;
}
-->
</style></head>
<body>
<?php
class SportObject{
const BOOK_TYPE = '计算机图书';
public $object_name;//图书名称
function setObjectName($name){//声明方法setObjectName()
$this -> object_name = $name;//设置成员变量值
}
function getObjectName(){//声明方法getObjectName()
return $this -> object_name;
}
}
$c_book = new SportObject();//实例化对象
$c_book -> setObjectName("PHP类");//调用方法setObjectName
echo SportObject::BOOK_TYPE."->";//输出常量BOOK_TYPE
echo $c_book -> getObjectName();//调用方法getObjectName
?>
</body>
</html>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>构造方法</title>
<style type="text/css">
<!--
body,td,th {
font-size: 12px;
}
body {
margin-left: 10px;
margin-top: 10px;
margin-right: 10px;
margin-bottom: 10px;
}
-->
</style></head>
<body>
<?php
class SportObject{
public $name;//定义成员变量
public $height;//定义成员变量
public $avoirdupois;//定义成员变量
public $age;//定义成员变量
public $sex;//定义成员变量
public function __construct($name,$height,$avoirdupois,$age,$sex){//定义构造方法
$this->name=$name;//为成员变量赋值
$this->height=$height;//为成员变量赋值
$this->avoirdupois=$avoirdupois;//为成员变量赋值
$this->age=$age;//为成员变量赋值
$this->sex=$sex;//为成员变量赋值
}
public function bootFootBall(){//声明成员方法
if($this->height<185 and $this->avoirdupois<85){
return $this->name.",符合踢足球的要求!";//方法实现的功能
}else{
return $this->name.",不符合踢足球的要求!";//方法实现的功能
}
}
}
$sport=new SportObject('1','185','80','20','男');//实例化类,并传递参数
echo $sport->bootFootBall();//执行类中的方法?></body>
</html>

析构方法,是对象被销毁时被调用的,作用是释放内存。

void __destruct ( void )
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>析构方法</title>
</head>
<body>
<?php
class SportObject{
public $name;//定义成员变量
public $height;//定义成员变量
public $avoirdupois;//定义成员变量
public $age;//定义成员变量
public $sex;//定义成员变量
public function __construct($name,$height,$avoirdupois,$age,$sex){//定义构造方法
$this->name=$name;//为成员变量赋值
$this->height=$height;//为成员变量赋值
$this->avoirdupois=$avoirdupois;//为成员变量赋值
$this->age=$age;//为成员变量赋值
$this->sex=$sex;//为成员变量赋值
}
public function bootFootBall(){//声明成员方法
if($this->height<185 and $this->avoirdupois<85){
return $this->name.",符合踢足球的要求!";//方法实现的功能
}else{
return $this->name.",不符合踢足球的要求!";//方法实现的功能
}
}
function __destruct(){
echo "<p><b>对象被销毁,调用析构函数。</b></p>";
}
}
$sport=new SportObject('1','185','80','20','男');//实例化类,并传递参数
//unset($sport);?>
</body>
</html>

继承和多态的实现

所有成员变量和方法

构造函数

先调用子类中的方法

去调用父类中的构造方法

class subClass extends superClass{

}

多态存在两种形式:覆盖和重载。

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>继承的实现</title>
<style type="text/css">
</head>
<body>
<?php
/* 父类 */
class SportObject{
public $name;//定义姓名成员变量
public $age;//定义年龄成员变量
public $avoirdupois;//定义体重成员变量
public $sex;//定义性别成员变量
public function __construct($name,$age,$avoirdupois,$sex){//定义构造方法
$this->name=$name;//为成员变量赋值
$this->age=$age;//为成员变量赋值
$this->avoirdupois=$avoirdupois;//为成员变量赋值
$this->sex=$sex;//为成员变量赋值
}
function showMe(){//定义方法
echo '这句话不会显示。';
}
}
/* 子类BeatBasketBall */
class BeatBasketBall extends SportObject{//定义子类,继承父类
public $height;//定义身高成员变量
function __construct($name,$height){//定义构造方法
$this -> height = $height;//为成员变量赋值
$this -> name = $name;//为成员变量赋值
}
function showMe(){//定义方法
if($this->height>185){
return $this->name.",符合打篮球的要求!";//方法实现的功能
}else{
return $this->name.",不符合打篮球的要求!";//方法实现的功能
}
}
}
/* 子类WeightLifting */
class WeightLifting extends SportObject{//继承父类
function showMe(){//定义方法
if($this->avoirdupois<85){
return $this->name.",符合举重的要求!";//方法实现的功能
}else{
return $this->name.",不符合举重的要求!";//方法实现的功能
}
}
}//实例化对象
$beatbasketball = new BeatBasketBall('','190');//实例化子类
$weightlifting = new WeightLifting('','185','80','20','男');
echo $beatbasketball->showMe()."<br>";//输出结果
echo $weightlifting->showMe()."<br>";
?>
</body>
</html>

继承的实现

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>继承的实现</title>
<style type="text/css">
</head>
<body>
<?php
/* 父类 */
class SportObject{
public $name;//定义姓名成员变量
public $age;//定义年龄成员变量
public $avoirdupois;//定义体重成员变量
public $sex;//定义性别成员变量
public function __construct($name,$age,$avoirdupois,$sex){//定义构造方法
$this->name=$name;//为成员变量赋值
$this->age=$age;//为成员变量赋值
$this->avoirdupois=$avoirdupois;//为成员变量赋值
$this->sex=$sex;//为成员变量赋值
}
function showMe(){//定义方法
echo '这句话不会显示。';
}
}
/* 子类BeatBasketBall */
class BeatBasketBall extends SportObject{//定义子类,继承父类
public $height;//定义身高成员变量
function __construct($name,$height){//定义构造方法
$this -> height = $height;//为成员变量赋值
$this -> name = $name;//为成员变量赋值
}
function showMe(){//定义方法
if($this->height>185){
return $this->name.",符合打篮球的要求!";//方法实现的功能
}else{
return $this->name.",不符合打篮球的要求!";//方法实现的功能
}
}
}
/* 子类WeightLifting */
class WeightLifting extends SportObject{//继承父类
function showMe(){//定义方法
if($this->avoirdupois<85){
return $this->name.",符合举重的要求!";//方法实现的功能
}else{
return $this->name.",不符合举重的要求!";//方法实现的功能
}
}
}//实例化对象
$beatbasketball = new BeatBasketBall('','190');//实例化子类
$weightlifting = new WeightLifting('','185','80','20','男');
echo $beatbasketball->showMe()."<br>";//输出结果
echo $weightlifting->showMe()."<br>";
?>
</body>
</html>

重载:

传递的参数个数不同,调用不同的方法,返回不同的值。

<?php
class C{
function __call($name,$num){//调用不存在的方法
echo "方法名称:" . $name . "<p>";//输出方法名
echo "参数存在个数:" . count($num) . "<p>";//输出参数个数
if (count($num) == 1){//根据参数个数调用不同的方法
echo $this->list1($a);
}
if (count($num) == 2){//根据参数个数调用不同的方法
echo $this->list2($a,$b);
}
}
public function list1($a){//定义方法
return "这是list1函数";
}
public function list2($a,$b){//定义方法
return "这是list2函数";
}
}
$a = new C;//类的实例化
$a->listshow(1,2);//调用方法,传递参数
?>
关键字::变量名/常量名/方法名
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>伪变量$this</title>
</head>
<body>
<?php
class example{
function exam(){
if(isset($this)){
echo '$this的值为:'.get_class($this);
}else{
echo '$this未定义';
}
}
}
$class_name = new example();
$class_name->exam();
?>
</body>
</html>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>“::”操作符</title>
</head>
<body>
<?php
class Book{
const NAME = 'computer';//常量NAME
function __construct(){//构造方法
echo '本月图书类冠军为:'.Book::NAME.' ';//输出默认值
}
}
class l_book extends Book{//Book类的子类
const NAME = 'foreign language';//声明常量
function __construct(){//子类的构造方法
parent::__construct();//调用父类的构造方法
echo '本月图书类冠军为:'.self::NAME.' ';//输出本类中的默认值
}
}
$obj = new l_book();//实例化对象
?>
</body>
</html>

parent: 可以调用父类中的成员变量,成员方法和常量。

self:可以调用当前类中的静态成员和常量。

类名:可以调用本类中的变量,常量和方法。

数据隐藏

public(公共成员)

private(私有成员)

protected(保护成员)

public, private, protected, static, final

静态变量(方法)

关键字::静态成员
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>private关键字</title>
</head>
<body>
<?php
class Book{
private $name = 'computer';//声明私有变量$name
public function setName($name){//设置私有变量
$this -> name = $name;
}
public function getName(){//读取私有变量
return $this -> name;
}
}
class LBook extends Book{//Book类的子类
}
$lbook = new LBook();//实例化对象
echo '正确操作私有变量的方法:';
$lbook -> setName("PHP");//对私有变量进行操作
echo $lbook -> getName();
echo '<br>直接操作私有变量的结果:';//对私有变量进行操作
echo Book::$name;
?>
</body>
</html>

private只能在所属类的内部被调和修改,不可以在类外被访问。

protected修饰的类成员,可以在本类和子类中被调用,其他地方不可以被调用。

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>protected关键字</title>
</head>
<body>
<?php
class Book{
protected $name = 'computer';//声明保护变量$name
}
class LBook extends Book{//Book类的子类
public function showMe(){
echo '对于protected修饰的变量,在子类中是可以直接调用的。如:$name = '.$this -> name;
}
}
$lbook = new LBook();//实例化对象
$lbook -> showMe();
echo '<p>但在其他的地方是不可以调用的,否则:';//对私有变量进行操作
$lbook -> name = 'history';
?>
</body>
</html>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>protected关键字</title>
</head>
<body>
<?php
class Book{
protected $name = 'computer';//声明保护变量$name
}
class LBook extends Book{//Book类的子类
public function showMe(){
echo '对于protected修饰的变量,在子类中是可以直接调用的。如:$name = '.$this -> name;
}
}
$lbook = new LBook();//实例化对象
$lbook -> showMe();
echo '<p>但在其他的地方是不可以调用的,否则:';//对私有变量进行操作
$lbook -> name = '会报错,这里就错了';
?>
</body>
</html>

静态变量:

关键字::静态成员

self,在类内部调用 静态成员所使用

在静态方法中,只能调用静态变量,不能调用普通变量,而普通方法可以调用静态变量。

对象被销毁后,仍然保存被修改的静态数据。

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>静态变量的使用</title>
</head>
<body>
<?php
class Book{//Book类
static $num = 0;//声明一个静态变量$num,初值为0
public function showMe(){//申明一个方法
echo '您是第'.self::$num.'位访客';//输出静态变量
self::$num++;//将静态变量加1
}
}
$book1 = new Book();//实例化对象$book1
$book1 -> showMe();//调用showMe()方法
echo "<br>";
$book2 = new Book();//实例化对象$book2;
$book2 -> showMe();//再次调用showMe()方法
echo "<br>";
echo '您是第'.Book::$num.'为访客';//直接使用类名调用静态变量
?>
</body>
</html>

运行效果是:

您是第0位访客

您是第1位访客

您是第2位访客

对象的高级应用

final关键字,抽象类,接口的使用,克隆对象,对象比较,对象类型检测,魔术方法。

不可以被继承,不能有子类
final class class_name{
//…
}final function method_name() 不可以重写和覆盖
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>final类</title>
></head>
<body>
<?php
final class SportObject{
function __construct(){
echo '不能被继承,不能有子类';
}
}
class MyBook extends SportObject{
static function exam(){
echo "会报错的";
}
}
MyBook::exam();
?>
</body>
</html>

抽象类,是不能被实例化的类,只能作为父类来使用:

abstract class AbstractName{

}

抽象类,一样有成员变量,成员方法,抽象类至少包含一个抽象方法,抽象方法没有方法体,其功能在子类完成。

abstract function abstractName();
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>抽象类</title>
</head>
<body>
<?php
abstract class CommodityObject{//定义抽象类
abstract function service($getName,$price,$num);//定义抽象方法
}
class MyBook extends CommodityObject{//定义子类,继承抽象类
function service($getName,$price,$num){//定义方法
echo '您购买的商品是'.$getName.',该商品的价格是:'.$price.' 元。';
echo '您购买的数量为:'.$num.' 本。';
echo '如发现缺页,请在3日内更换。';
}
}
class MyComputer extends CommodityObject{//定义子类继承父类
function service($getName,$price,$num){//定义方法
echo '您购买的商品是'.$getName.',该商品的价格是:'.$price.' 元。';
echo '您购买的数量为:'.$num.' 台。';
echo '如发生非人为质量问题,请在3个月内更换。';
}
}
$book = new MyBook();//实例化子类
$computer = new MyComputer();//实例化子类
$book -> service('PHP',5,3);//调用方法
echo '<p>';
$computer -> service('XX本',800,1);//调用方法?>
</body>
</html>

接口

interface InterfaceName{
function interfaceName1();
function interfaceName2();

}class SubClass implements InterfaceName1,InterfaceName2{
function interfaceName1(){//功能实现
}
function interfaceName2(){//功能实现
}

}
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>接口</title>
</head>
<body>
<?php
interface MPopedom{
function popedom();
}
interface MPurview{
function purview();
}
class Member implements MPurview{
function purview(){
echo '会员';
}
}
class Manager implements MPurview,MPopedom{
function purview(){
echo '管理员拥有全部权限。';
}
function popedom(){
echo '管理员还没有的权限';
}
}
$member = new Member();
$manager = new Manager();
$member -> purview();
echo '<p>';
$manager -> purview();
$manager ->popedom();
?>
</body>
</html>

克隆对象

$object1 = new ClassName();
$object2 = clone $object1;

__clone()方法,调用__clone()方法,可以克隆出来的对象的一些行为及属性

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>对象的引用</title>
</head>
<body>
<?php
class SportObject{//类SportObject
private $object_type = 'book';//声明私有变量$object_type,并赋初值等于“book”
public function setType($type){//声明成员方法setType,为变量$object_type赋值
$this -> object_type = $type;
}
public function getType(){//声明成员方法getType,返回变量$object_type的值
return $this -> object_type;
}
}
$book1 = new SportObject();//实例化对象$book1
$book2 = $book1;//使用普通数据类型的方法给对象$book2赋值
$book2 -> setType('computer');//改变对象$book2的值
echo '对象$book1的值为:'.$book1 -> getType();//输出对象$book1的值
?></body>
</html>

对象比较

两个等号“”是比较两个对象的内容

3个等号“=”是比较对象的引用地址

对象类型检测

instanceof操作符可以检测当前对象是属于哪个类

ObjectName instanceof ClassName

魔术方法

__construct()、__destruct()和__clone()
__set()和__get()方法
__call()方法
__sleep()和__wakeup()方法
__toString()方法
__autoload()方法

__call()方法包含两个参数,即方法名和方法参数

方法参数是以数组形式存在的

_set()方法包含两个参数,分别表示变量名称和变量值

__get()方法有一个参数,表示要调用的变量名

使用serialize()函数可以实现序列化对象

unserialize()函数可以重新还原一个被serialize()函数序列化的对象

__sleep()方法可以清除对象并返回一个该对象中所有变量的数组

__wakeup()方法则是恢复在序列化中可能丢失的数据库连接及相关工作

__toString()方法将对象转化为字符串

对象的比较

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>对象的比较</title>
</head>
<body>
<?php
class SportObject{
private $name;
function __construct($name){
$this -> name = $name;
}
}
$book = new SportObject('book');
$cloneBook = clone $book;
$referBook = $book;
if($cloneBook == $book){
echo '两个对象的内容相等<br>';
}
if($referBook === $book){
echo '两个对象的引用地址相等<br>';
}
?>
</body>
</html>

对象的比较

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>对象的比较</title>
</head>
<body>
<?php
class SportObject{
private $name;
function __construct($name){
$this -> name = $name;
}
}
$book = new SportObject('book');
$cloneBook = clone $book;
$referBook = $book;
if($cloneBook == $book){
echo '两个对象的内容相等<br>';
}
if($referBook === $book){
echo '两个对象的引用地址相等<br>';
}
?>
</body>
</html>

对象类型检测

instanceof操作符可以检测当前对象是属于哪个类

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>对象类型检测</title>
</head>
<body>
<?php
class SportObject{}
class MyBook extends SportObject{
private $type;
}
$cBook = new MyBook();
if($cBook instanceof MyBook)
echo '对象$cBook属于MyBook类<br>';
if($cBook instanceof SportObject)
echo '对象$cBook属于SportObject类<br>';
?>
</body>
</html>

测试结果:

对象$cBook属于MyBook类对象$cBook属于SportObject类

魔术方法:

__set()和__get()方法

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>__set()和__get()方法</title>
</head>
<body>
<?php
class SportObject
{
private $type = ' ';
public function getType(){
return $this -> type;
}
private function __get($name){
if(isset($this ->$name)){
echo '变量'.$name.'的值为:'.$this -> $name.'<br>';
}
else{
echo '变量'.$name.'未定义,初始化为0<br>';
$this -> $name = 0;
}
}
private function __set($name, $value){
if(isset($this -> $name)){
$this -> $name = $value;
echo '变量'.$name.'赋值为:'.$value.'<br>';
}else{
$this -> $name = $value;
echo '变量'.$name.'被初始化为:'.$value.'<br>';
}
}
}
$MyComputer = new SportObject();
$MyComputer -> type = 'DIY';
$MyComputer -> type;
$MyComputer -> name;
?>
</body>
</html>

变量type赋值为:DIY

变量type的值为:DIY

变量name未定义,初始化为0

变量name被初始化为:0

__call()方法

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>__call()方法</title>
</head>
<body>
<?php
class SportObject
{
public function myDream(){
echo '调用的方法存在,直接执行此方法。<p>';
}
public function __call($method, $parameter)
{
echo '如果方法不存在,则执行__call()方法。<br>';
echo '方法名为:'.$method.'<br>';
echo '参数有:';
var_dump($parameter);
}
}
$MyLife = new SportObject();
$MyLife -> myDream();
$MyLife -> mDream('how','what','why');
?>
</body>
</html>

__sleep()和__wakeup()方法

__sleep()方法可以清除对象并返回一个包含对象中所有变量的数组。

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>__sleep()和__wakeup()方法</title>
</head>
<body>
<?php
class SportObject{
private $type = 'DIY';
public function getType(){
return $this -> type;
}
public function __sleep(){
echo '使用serialize()函数将对象保存起来,可以存放到文本文件、数据库等地方<br>';
return $this;
}
public function __wakeup(){
echo '当需要该数据时,使用unserialize()函数对已序列化的字符串进行操作,将其转换回对象<br>';
}
}
$myBook = new SportObject();
$i = serialize($myBook);
echo '序列化后的字符串:'.$i.'<br>';
$reBook = unserialize($i);
echo '还原后的成员变量:'.$reBook -> getType();
?>
</body>
</html>

__toString()方法

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>__toString()方法</title>
</head>
<body>
<?php
class SportObject{
private $type = 'DIY';
public function __toString(){
return $this -> type;
}
}
$myComputer = new SportObject();
echo '对象$myComputer的值为:';
echo $myComputer;
?>
</body>
</html>

__autoload()方法自动实例化需要使用的类

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>__autoload()方法自动实例化需要使用的类</title>
</head>
<body>
<?php
function __autoload($class_name){
$class_path = $class_name.'.class.php';
if(file_exists($class_path)){
include_once($class_path);
}else
echo '类路径错误。';
}
$myBook = new SportObject("江山代有人才出 各领风骚数百年");
echo $myBook;
?>
</body>
</html><?php
class SportObject{
private $cont;
public function __construct($cont){
$this -> cont = $cont;
}
public function __toString(){
return $this -> cont;
}
}
?>

中文字符串的截取类

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>中文字符串的截取类</title>
</head>
<body>
<?php
class MsubStr{
function csubstr($str, $start, $len) { //$str指的是字符串,$start指的是字符串的起始位置,$len指的是长度。
$strlen = $start + $len; //用$strlen存储字符串的总长度(从字符串的起始位置到字符串的总长度)
for($i = 0; $i < $strlen; $i ++) { //通过for循环语句,循环读取字符串
if (ord ( substr ( $str, $i, 1 ) ) > 0xa0) { //如果字符串中首个字节的ASCII序数值大于0xa0,则表示为汉字
$tmpstr .= substr ( $str, $i, 2 ); //每次取出两位字符赋给变量$tmpstr,即等于一个汉字
$i ++; //变量自加1
} else { //如果不是汉字,则每次取出一位字符赋给变量$tmpstr
$tmpstr .= substr ( $str, $i, 1 );
}
}
return $tmpstr; //输出字符串
}
}
$mc=new MsubStr();//类的实例化
?>
<table width="204" height="195" border="0" cellpadding="0" cellspacing="0" background="images/bg.JPG">
<tr>
<td width="25" height="37">&nbsp;</td>
<td width="157">&nbsp;</td>
<td width="22">&nbsp;</td>
</tr>
<tr>
<td height="30">&nbsp;</td>
<td><?php
$string="关注!";
if(strlen($string)>10){
echo substr($string,0,9)."...";
}else{
echo $string;
}
?>
</td>
<td>&nbsp;</td>
</tr>
<tr>
<td height="30">&nbsp;</td>
<td><?php
$string="关注";
if(strlen($string)>10){
echo substr($string,0,9)."...";
}else{
echo $string;
}
?></td>
<td>&nbsp;</td>
</tr>
<tr>
<td height="30">&nbsp;</td>
<td>
<?php
$strs="关注";
if(strlen($string)>10){
echo $mc ->csubstr($strs, "0" , "9")."...";
}else{
echo $strs;
}
?>
</td>
<td>&nbsp;</td>
</tr>
<tr>
<td height="30">&nbsp;</td>
<td><?php
$strs="关注";
if(strlen($string)>30){
echo $mc ->csubstr($strs, "0" , "20")."...";
}else{
echo $strs;
}
?>
</td>
<td>&nbsp;</td>
</tr>
<tr>
<td height="38">&nbsp;</td>
<td>&nbsp;</td>
<td>&nbsp;</td>
</tr>
</table>
</body>
</html><?php
class MsubStr{
function csubstr($str, $start, $len) { //$str指的是字符串,$start指的是字符串的起始位置,$len指的是长度。
$strlen = $start + $len; //用$strlen存储字符串的总长度(从字符串的起始位置到字符串的总长度)
for($i = 0; $i < $strlen; $i ++) { //通过for循环语句,循环读取字符串
if (ord ( substr ( $str, $i, 1 ) ) > 0xa0) { //如果字符串中首个字节的ASCII序数值大于0xa0,则表示为汉字
$tmpstr .= substr ( $str, $i, 2 ); //每次取出两位字符赋给变量$tmpstr,即等于一个汉字
$i ++; //变量自加1
} else { //如果不是汉字,则每次取出一位字符赋给变量$tmpstr
$tmpstr .= substr ( $str, $i, 1 );
}
}
return $tmpstr; //输出字符串
}
}?>
<?php
class changecode{
private $str;
private $input;
private $output;
function __construct($input='',$output='gb2312'){
$this->input = $input;
$this->output = $output;
}
protected function chgcode($str){
if($this->input != ''){
$this->str = iconv($input,$output,$str);
}else{
$this->str = mb_convert_encoding($str,$output);
}
}
function getStr($str){
$this->chgcode($str);
return $this->str;
}
function setInput($input){
$this->input = $input;
}
function setOutput($output){
$this->output = $output;
}
function getInput(){
return $this->input;
}
function getOutput(){
return $this->output;
}
}
?>

结言

好了,欢迎在留言区留言,与大家分享你的经验和心得。

感谢你学习今天的内容,如果你觉得这篇文章对你有帮助的话,也欢迎把它分享给更多的朋友,感谢。

感谢!承蒙关照!您真诚的赞赏是我前进的最大动力!

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,492
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,907
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,740
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,495
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:8,132
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,295