Home  >  Article  >  php教程  >  php面向对象编程学习笔记

php面向对象编程学习笔记

WBOY
WBOYOriginal
2016-06-13 10:16:10858browse

面向对象编程是php中一种常用的使用方法,本文章来介绍php面向对象简单使用方法与一些基本知识有需要的朋友可进入参考。

(OOP)来开发。面向对象开发相对于面向过程有很多优点:
维护简单   模块化是面向对象编程中的一个特征。实体被表示为类和同一名字空间中具有相同功能的类,我们可以在名字空间中添加一个类而不会影响该名字空间的其他成员。
  可扩充性   面向对象编程从本质上支持扩充性。如果有一个具有某种功能的类,就可以很快地扩充这个类,创建一个具有扩充的功能的类。
代码重用   由于功能是被封装在类中的,并且类是作为一个独立实体而存在的,提供一个类库就非常简单了。
它比较适合多人合作来开发项目,所以现在很多大中型网站都选择了用OOP来开发。

下面我来来介绍面向对象编程

类与属性和方法

PHP中定义类语法格式:

 代码如下 复制代码
class classname [可选属性]{
public $property [=value];… //用public声明一个公共标识 然后给予一个变量 变量也可以赋值
function functionname ( args ){ //类的方法里的成员函数
代码} …
//类的方法(成员函数)
}

生成对象(类的实例化): $对象名=new classname( );
使用对象的属性

在一个类中,可以访问一个特殊指针$this当在该类中通过一个操作设置或访问该变量时,使用$this->name来引用.

 代码如下 复制代码

class person{
function _ _destruct( )
{ echo "bye bye !“; }
}
$a=new person();


1.final
final:php5新增一个final关键字。如果父类中的方法被声明为final,则子类无法覆盖该方法;如果一个类被声明final,则不能被继承。

 代码如下 复制代码
 
class BaseClass{
     public function test(){
          ehco "test";
     }
 
     final public function moreTest(){
          echo "moretest";
     }
}
 
class ChildClass extends BaseClass{
     public function moreTest(){
          echo "moretest";
     }
}
// 产生 Fatal error: Cannot override final method BaseClass::moretest()

 
2.__toString(建议用PHP5.2或者更高版本)

 代码如下 复制代码
class Person{
     protected $name;
     protected $email;
    
     public function setName($name){
          $this->name = $name;
     }
 
     public function setEmail($email){
          $this->email = $email;
     }
 
     public function __toString(){
          return "$this->name email>";
     }
}
$rasums = new Person;
$rasums->setName('test');
$rasums->setEmail('test@qq.com');
print $rasums;


 
3.接口和抽象类

接口的作用:你想要保证一个类按照特定的名称、可见性和原型实现一个或多个方法。
接口的要求:
     类中全部为抽象方法
     抽象方法钱不用加abstract
     接口抽象方法属性为public
     成员属性必须为常量
例:

 代码如下 复制代码
interface ChildTest{
     public function childTest();
}
class FathTest implements ChildTest1,ChildTest2{
     public function childTest(){
          echo 1;
     }
     …………
}

 
抽象的作用: 其实抽象类和接口类有一部分很像,记得在哪里看见这样一句话,抽象类就把类像的部分抽出来,这句看上去很搞笑,其实它说出了抽象类的真理,抽象类的作用 是,当你发现你的很多类里面用很多方法你不断的在重复写,那你就可以考虑使用抽象类了,你可能会说“我不是可以重写一个类每个公共类我个实例化一个这个公 共类,调用相同的方法就可以了”,这里是可以,实际上抽象类做的工作也就是这个,不过他省去了你实例化的这个步骤,让你就像直接调用本类方法一样方便,而 且你还可以重载这个方法。
抽象的要求:
     类中至少有一个抽象方法
     抽象方法钱必须加abstract
例:

 代码如下 复制代码
abstract class Database{
     abstract public function connect();
     abstract public function query();
     abstract public function fetch();
     abstract public function close();
}

注:抽象方法不能定义为私有方法、不能定义为最终方法,因为它们需要被继承。
 
4.传递对象引用
php4:所有“=”都是创建一个副本
php5:除了对象外,其他“=”进行赋值时,都是创建一个副本;而对象则是引用
 
5.克隆对象
一、
聚合类:
__call方法简介:
当客户端代码用类中未定义的方法时,__call会被调用。
__call()接受两个参数,一个是方法名称,另一个是传递给要调用方法的所有参数(包括数组)
__call()方法返回的任何值都会返回给客户,将好像调用方式真实存在一样
例:

 代码如下 复制代码

class Address{
     protected $city;
     protected $country;

     public function setCity($city){$this->city = $city;}
     public function getCity(){return $this->city;}
     public function setCountry($country){$this->country = $country;}
     public function getCountry(){return $this->country;}
}

class Person{
     protected $name;
     protected $address;
     //浅克隆
     public function __construct(){
          $this->address = new Address;
     }

     public function setName($name){
          $this->name = $name;
     }
     public function getName(){
          return $this->name;
     }

     public function __call($method,$arguments){
          if(method_exists($this->address,$method)){
               return call_user_func_array(array($this->address,$method),$arguments);
          }
     }
     //深克隆
     public function __clone(){
          $this->address = clone $this->address;
     }
}

$test1 = new Person;
$test2 = clone $test1;

$test1->setName('testname1');
$test1->setCity('testcity1');
$test2->setName('testname2');
$test2->setCity('testcity2');

echo $test1->getName().'-'.$test1->getCity()."n";
echo $test2->getName().'-'.$test2->getCity()."n";
//testname1-testcity2
//testname2-testcity2


 
6.重要属性访问(__set __get __isset __unset) __isset __unset5.1之后才有用
作用:拦截对属性的需求,为了提高分离的程度,还要实现__isset()和__unset(),以便当我们用isset来检测属性或者unset()来删除属性,来保证类的行为正确
例:

 代码如下 复制代码

class Person{
     protected $__data = array('email','test');

     public function __get($property){
          if(isset($this->__data[$property])){
               return $this->__data[$property];
          }else{
               return false;
          }
     }

     public function __set($property,$value){
          if(isset($this->__data[$property])){
               return $this->__data[$property] = $value;
          }else{
               return false;
          }
     }
 
     public function __isset($property){
          if(isset($this->__data[$property])){
               return true;
          }else{
               return false;
          }
     }
 
     public function __unset($property){
          if(isset($this->__data[$property])){
               return unset($this->__data[$property]);
          }else{
               return false;
          }
     }
}

$test = new Person;
$test->email= 'test';
var_dump($test->email);

注意:

这两个方法只会捕捉缺少的属性,如果你为你的类定义了一个属性,那么当访问这个属性时php不会调用__get()和__set();
     这两个方法完全破坏了任何属性继承的想法。如果父对象中有个 __get()方法,而你在子类中又实现了自己的__get()方法,那么你的对象不会正确的执行,因为父类的__get()方法永远不会被调用,当然可以用parent::__get()解决

缺点:

速度相对较慢
使用魔术访问器方法就不可能在使用反射类,如phpdocumentor这类的工具将代码自动文档化
不能将其用于静态属性

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn