Go into php self and $this

  • 2020-06-07 04:07:54
  • OfStack

First, parent and self:

<?php
/*
 * Created by YinYiNiao
 */
 class A{
  function __construct(){
   echo " The base class A The construction method of <br />";
  }
 }
 class B extends A{
  function __construct(){
   parent::__construct();
   echo " A subclass B The construction method of <br />";
   self::myFun();
  }
  function myfun(){
   echo "1 A common method myFun()<br />";
  }
 }
$obj=new A();
$obj=new B();
?>

self and $this function very similar, but they are not the same. $this cannot refer to static members and constants. self is more like a class capability, while $this is more like the instance itself.