php class中self,parent,this的区别,以及实例

我容易混淆public,private,protected,还容易混淆this,self这些东西。前面已经写了一篇关于public,private,protected博文了,下面来说一下this,self,parent的用法

一,this

1,要用this,你必有是一个对像的形势,不然它会报错的,Fatal error: Using $this when not in object context。
2,this可以调用本类中的方法和属性,也可以调用父类中的可以调的方法和属性

二,self

1,self可以访问本类中的静态属性和静态方法,可以访问父类中的静态属性和静态方法。
2,用self时,可以不用实例化的


三,parent

1,parent可以访问父类中的静态属性和静态方法。
2,用parent时,可以不用实例化的

四,实例

查看复制打印?
  1.   
  2. class test{  
  3.  public $public;  
  4.  private $private;  
  5.  protected $protected;  
  6.  static $instance;  
  7.  static $good = 'tankzhang 
    '
    ;  
  8.  public $tank = 'zhangying 
    '
    ;  
  9.   
  10.  public  function __construct(){  
  11.  $this->public    = 'public     
    '
    ;  
  12.  $this->private   = 'private    
    '
    ;  
  13.  $this->protected = 'protected  
    '
    ;  
  14.   
  15.  }  
  16.  public function tank(){                          //私有方法不能继承,换成public,protected  
  17.  if (!isset(self::$instance[get_class()]))  
  18.  {  
  19.  $c = get_class();  
  20.  self::$instance = new $c;  
  21.  }  
  22.  return self::$instance;  
  23.  }      
  24.   
  25.  public function pub_function() {  
  26.  echo "you request public function
    "
    ;  
  27.  echo $this->public;  
  28.  }  
  29.  protected  function pro_function(){  
  30.  echo "you request protected function
    "
    ;  
  31.  echo $this->protected;  
  32.  }  
  33.  private function pri_function(){  
  34.  echo "you request private function
    "
    ;  
  35.  echo $this->private;  
  36.  }  
  37.  static function sta_function(){  
  38.  echo "you request static function
    "
    ;  
  39.  }  
  40. }  
  41.   
  42. class test1 extends test{  
  43.   
  44.  static $love = "tank 
    "
    ;  
  45.  private $aaaaaaa = "ying 
    "
    ;  
  46.   
  47.  public function __construct(){  
  48.  parent::tank();  
  49.  parent::__construct();  
  50.  }  
  51.  public function tank(){  
  52.  echo $this->public;  
  53.  echo $this->protected;  
  54.  echo $this->aaaaaaa;  
  55.  $this->pro_function();  
  56.  }  
  57.   
  58.  public  function test1_function(){  
  59.  echo self::$love;  
  60.  echo self::$good;  
  61.  echo parent::$good;  
  62.  echo parent::$tank;   //Fatal error: Access to undeclared static property: test::$tank  
  63.  echo self::$tank;     //Fatal error: Access to undeclared static property: test::$tank  
  64.  }  
  65.  static function extends_function(){  
  66.  parent::sta_function();  
  67.  self::pro_function();  
  68.  echo "you request extends_private function
    "
    ;  
  69.  }  
  70. }  
  71.   
  72. error_reporting(E_ALL);  
  73. $test = new test1();  
  74. $test->tank();            //子类和父类有相同名字的属性和方法,实例化子类时,会调用子类中的方法。  
  75. test1::test1_function();  
  76. test1::extends_function();  //执行一部分后,报Fatal error: Using $this when not in object context in D:\xampp\htdocs\mytest\www4.php on line 32  
  77. ?>  

1,当我们调用$test->tank();这个方法时,tank里面的$this是一个对像,这个对像可以调用本类,父类中的方法和属性,

2,test1::test1_function();当我们用静态的方法去调用非静态方法时,会显示警告的,Non-static method test::test1_function() should not be called statically可以看出不,self可以调用本类,父类中的静态属性,parent可以调用父类中的静态属性,二者调用非静态属性会报错。代码中有注释

3,test1::extends_function();这一步会报错,报在非对像中使用$this。为什么会这样呢,test1::extends_function();只是调用了class中的一个方法,并没有实例化,所以根本不存在什么对像,当父类中用到$this时,就会报错

你可能感兴趣的:(PHP+MYSQL)