Home  >  Article  >  Backend Development  >  php-object-oriented (2)

php-object-oriented (2)

WBOY
WBOYOriginal
2016-08-08 09:19:24817browse

1. Review: In the previous article, I learned some basic knowledge of object-oriented, including the use of $this

2. In this article, I will learn PHP object-oriented inheritance, properties, static properties and methods

3 .Inherit (extends)

     (1)一个类可以在声明用extends关键字继承另一类的方法和成员
     (2)不过扩展多少个类,只能继承一个基类
     (3)被继承的方法和成员可以通过用同样的名字重新声明被覆盖
     (4)若父类定义方法使用了final关键字,则不可覆盖
     (5)可以通过parent::来访问被覆盖的父类方法和成员
<pre name="code"> class aclass{

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

            public function displayVar(){
             echo "我是 bclass";
             parent::displayVar();
          }
      }
  
      $f=new bclass();  
      $f->displayVar(); #结果: 我是 bclass 我是成员变量我是t1

4. Properties
<pre class="brush:php;toolbar:false"> (1)类的成员变量为属性/字段/特征。一般使用属性。
 (2)属性声明:
          public:类成员可以在任何地方被访问;
          protected :可以被其所在类的子类和父类访问(当然,该成员所在的类也可以访问)
          private  :能被其所在类访问。
          var :可以放在public,prorected,private前面,也可直接声明属性:默认是public
 (3)属性变量可以进行初始化,但初始化,必须是常数
 (4)在类成员方法里,可通过 $this->属性/方法名 来访问属性/方法
<pre name="code"> class cclass{
         public $var1="hello";
         public $var2=array(TRUE,FALSE);
      }
        #在php5.3 后,可以使用nowdoc初始化属性
        public $var3=<<<&#39;yuan&#39;;

5. Static properties and methods Static
<pre class="brush:php;toolbar:false"><span></span>    (1)声明类成员或方法为static,就可以不实例化类,而直接访问。
     (2)属性和方法默认为public
     (3)伪变量$this 在静态方法中不可用
     (4)静态属性只能初始化为一个字符&#20540;或一个常量/整型或数组
     (5)用::方式调用静态方法或属性
<pre name="code">     #静态属性
     class dclass{
         public static $var4="我是static 属性";
         public function staticValue(){
             //类本身调用静态属性
             return self::$var4;
         }
     }

     class eclass extends dclass{
         public function efun(){
             //调用父类静态属性
             return parent::$var4;
         }
     }

     echo dclass::$var4;  //结果:我是static 属性
     
     $e=new eclass();
     echo $e->staticValue(); //结果:我是static 属性
     echo $e->efun();   //结果:我是static 属性

     echo $e::$var4; //结果:我是static 属性

     #php5.3.0后的支持
     $classname='dclass';
     echo $classname::$var4;//结果:我是static 属性

     echo eclass::$var4; //结果:我是static 属性

     echo $e->$var4;  ////结果:我是static 属性

     #静态函数/方法
     class fclass{
         public static function ffun(){
             //....
         }
     }

 小结:在学习过程中,发现和java,c#很多相同的知识,所有速度有些kuai!

 下篇学习php类常量,自动加载类,构造函数和析构函数!

Copyright statement: This article is original by the blogger Article, without blogging No reproduction allowed with permission of the owner.

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

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