深入php self与$this的详解_php技巧

WBOY
Release: 2016-05-17 09:02:41
Original
887 people have browsed it

先谈parent与self:

复制代码 代码如下:

/*
 * Created by YinYiNiao
 */
 class A{
  function __construct(){
   echo "基类A的构造方法
";
  }
 }
 class B extends A{
  function __construct(){
   parent::__construct();
   echo "子类B的构造方法
";
   self::myFun();
  }
  function myfun(){
   echo "一个普通方法myFun()
";
  }
 }
$obj=new A();
$obj=new B();
?>

self与$this的功能极其相似,但二者又不相同。$this不能引用静态成员和常量。self更像类本事,而$this更像是实例本身。
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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!