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

yipeiwu_com5年前PHP代码库

一,this

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

二,self

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


三,parent

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

复制代码 代码如下:

<?php

class test{
 public $public;
 private $private;
 protected $protected;
 static $instance;
 static $good = 'tankzhang <br>';
 public $tank = 'zhangying <br>';

 public  function __construct(){
 $this->public    = 'public     <br>';
 $this->private   = 'private    <br>';
 $this->protected = 'protected  <br>';

 }
 public function tank(){                          //私有方法不能继承,换成public,protected
 if (!isset(self::$instance[get_class()]))
 {
 $c = get_class();
 self::$instance = new $c;
 }
 return self::$instance;
 }   

 public function pub_function() {
 echo "you request public function<br>";
 echo $this->public;
 }
 protected  function pro_function(){
 echo "you request protected function<br>";
 echo $this->protected;
 }
 private function pri_function(){
 echo "you request private function<br>";
 echo $this->private;
 }
 static function sta_function(){
 echo "you request static function<br>";
 }
}

class test1 extends test{

 static $love = "tank <br>";
 private $aaaaaaa = "ying <br>";

 public function __construct(){
 parent::tank();
 parent::__construct();
 }
 public function tank(){
 echo $this->public;
 echo $this->protected;
 echo $this->aaaaaaa;
 $this->pro_function();
 }

 public  function test1_function(){
 echo self::$love;
 echo self::$good;
 echo parent::$good;
 echo parent::$tank;   //Fatal error: Access to undeclared static property: test::$tank
 echo self::$tank;     //Fatal error: Access to undeclared static property: test::$tank
 }
 static function extends_function(){
 parent::sta_function();
 self::pro_function();
 echo "you request extends_private function<br>";
 }
}

error_reporting(E_ALL);
$test = new test1();
$test->tank();            //子类和父类有相同名字的属性和方法,实例化子类时,会调用子类中的方法。
test1::test1_function();
test1::extends_function();  //执行一部分后,报Fatal error: Using $this when not in object context in D:\xampp\htdocs\mytest\www4.php on line 32
?>

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时,就会报错

相关文章

搭建Vim为自定义的PHP开发工具的一些技巧

搭建Vim为自定义的PHP开发工具的一些技巧

虽然 vim 本质上只是一个编辑器。但只要配合一些适当的插件, vim 也能变成一个全功能的 IDE 。笔者使用 vim 已经有挺长一段时间了,经过反复的试验,配置了一个高效的 PHP...

php封装db类连接sqlite3数据库的方法实例

前言 SQLite3扩展名在PHP 5.3.0+以上都会默认启用。可以在编译时使用--without-sqlite3来禁用它。 Windows用户可通过启用php_sqlite3.dll...

php模块memcache和memcached区别分析

1.目前大多数php环境里使用的都是不带d的memcache版本,这个版本出的比较早,是一个原生版本,完全在php框架内开发的。与之对应的带d的memcached是建立在libmemca...

PHP简单实现数字分页功能示例

PHP简单实现数字分页功能示例

本文实例讲述了PHP简单实现数字分页功能。分享给大家供大家参考,具体如下: <?php header ( 'Content-Type: text/html; chars...

php读取txt文件并将数据插入到数据库

今天测试一个功能,需要往数据库中插入一些原始数据,PM给了一个txt文件,如何快速的将这个txt文件的内容拆分为所要的数组,然后再插入到数据库中? serial_number.txt的...