Home > Backend Development > PHP Tutorial > php-object-oriented (1)

php-object-oriented (1)

WBOY
Release: 2016-08-08 09:19:23
Original
907 people have browsed it

1. Review: In the previous article, I learned the filters in PHP Advanced

2. Starting from this article, I will learn PHP - object-oriented

3. Object-oriented

3.1 Understanding

   (1)php5 引入了新的对象模型 (object model),重写了php的处理对象方式,允许更加的性能
   (2)新特性包括:可见性,抽象类和final类,类方法,魔术方法,接口,对象克隆和类型提示
   (3)php对象是按引用传递的,即每个包含对象的变量都持有对象的引用(reference),而不是整个对象的拷贝
Copy after login
Copy after login
  3.2基本概念
Copy after login
<pre class="brush:php;toolbar:false">   (1) 关键字 class开头,后面跟着类名,可以是任何非php保留字的名字
   (2) 包含类成员和方法的定义
   (3) 伪变量$this 可以在当一个方法在对象内调用时使用。
   (4) $this是一个到调用对象的引用
   (5) 通常方法所属于对象,但也可以是另一个对象,如果该方法是从第二个对象内静态调用的话
Copy after login

3.3 Use of $this

$this的使用
     class one{
         function oneFun(){
             if(isset($this)){
                 echo '$this 的定义是 (';
                 echo  get_class($this);
                 echo ')';
             }else{
                 echo '$this 没有定义';
             }
         }
     }

     class two{
         function twoFun(){
             one::oneFun();
         }
     }

     $a =new one();
     $a->oneFun(); //结果:$this 的定义是 (one)

     one::oneFun();//结果:$this 没有定义

     $b=new two();
     $b->twoFun(); //结果:$this 的定义是 (two)

     two::twoFun();//结果:$this 没有定义
Copy after login

3.4. Simple class definition

 class aclass{

         //成员变量
         public $var="我是成员变量";
         public $t1="我是t1";
         //成员函数/方法
         public function displayVar(){
             echo "<br>";
             echo $this->var;
             echo $this->t1;
         }
     }
Copy after login

3.5 new keyword

(1)When creating an object instance, you must create a new object and It is assigned to a variable

     (2)每次创建新对象时,该对象总是被赋值,除非构函数出错
Copy after login

//调用
      $c=new aclass();
      $c->displayvar();
Copy after login

3.6 Object assignment

When assigning an instance of an object to a new variable, the new variable will Access the same instance

      #当把一个对象已经创建的实例赋值给新变量的时候,新变量会访问同一个实例
      $d=$c;
      $e =& $c;

      $c->var='$d 有这个值';

      $c=NULL; 
      echo "<br>";
      var_dump($c);  //结果:NULL
      var_dump($d);  //结果:object(aclass)#3 (2) { ["var"]=> string(15) "$d 有这个值" ["t1"]=> string(8) "我是t1" }
      var_dump($e);  //结果:NULL
Copy after login

The next article will learn about inheritance, properties and static properties/methods (static)

Copyright statement: This article is an original article by the blogger and may not be reproduced without the permission of the blogger.

The above introduces php-object-oriented (1), including aspects of content. I hope it will be helpful to friends who are interested in PHP tutorials.

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template