我容易混淆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时,可以不用实例化的

四,实例

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

self,parent,this区别的更多相关文章

  1. php中的public、protected、private三种访问控制模式及self和parent的区别(转)

    php的public.protected.private三种访问控制模式的区别 public: 公有类型 在子类中可以通过self::var调用public方法或属性,parent::method调用 ...

  2. PHP中this,self,parent的区别

    {一}PHP中this,self,parent的区别之一this篇 面向对象编程(OOP,Object OrientedProgramming)现已经成为编程人员的一项基本技能.利用OOP的思想进行P ...

  3. document.referrer的使用和window.opener 跟 window.parent 的区别

    偶尔看到了document.referrer,之前一直有点疑惑与window.opener 和 window.parent之间的区别 首先查了一下w3cSCHOOL, 上面的解释:referrer 属 ...

  4. 转 PHP编程过程中需要了解的this,self,parent的区别

    {一}PHP中this,self,parent的区别之一this篇 面向对象编程(OOP,Object Oriented Programming)现已经成为编程人员的一项基本技能.利用OOP的思想进行 ...

  5. php的self this parent的区别

    {一}PHP中this,self,parent的区别之一this篇 面向对象编程(OOP,Object OrientedProgramming)现已经成为编程人员的一项基本技能.利用OOP的思想进行P ...

  6. 转: PHP中this,self,parent的区别

    {一}PHP中this,self,parent的区别之一this篇 面向对象编程(OOP,Object OrientedProgramming)现已经成为编程人员的一项基本技能.利用OOP的思想进行P ...

  7. 【PHP】this,self,parent的区别(转)

    一. 定义&区别 self: 指向当前类的指针,self是不指向任何已经实例化的对象,一般self使用来指向类中的静态变量. this: 指向当前对象的指针,使用parent来调用父类的构造函 ...

  8. 教程-关于Owner和Parent的区别

    Parent属性是指构件的包容器,构件只能在此范围内显示和移动 Owner属性是指构件的所有者,它负责构件的创建和释放.

  9. PHP面向对象中 static:: 与 self:: parent:: $this-> 的区别

    很多好几年工作经验的PHP工程师,对PHP面向对象中 static:: .self::.parent::.$this->  的定义和使用都不清晰,特做详细梳理: static:: 可以访问全局作 ...

随机推荐

  1. python 基础——多重继承

    原始的初始化 子类直接调用超类 __init__ 方法初始化,当形成钻石继承的时候,超类会被多次初始化,可能会有意向不到的问题: BaseClass /        \ OneClass    Tw ...

  2. 查询score中选学多门课程的同学中分数为非最高分成绩的记录。

    20.查询score中选学多门课程的同学中分数为非最高分成绩的记录. select * from score a where sno in ( select sno from score group ...

  3. Differential Geometry之第二章曲线的局部理论

    第二章.曲线的局部理论 2.1 曲线的概念 关于非正则曲线的讨论: ,这是个非正则点(尖点),且它是非正则曲线. 直观上,间断点,孤立点,结点(交叉点),尖点是非正则点. 有记载说:当同一条曲线用不同 ...

  4. tmux使用

    1.配置文件的使用 在~/.tmux.conf中添加: setw -g mouse-resize-pane on setw -g mouse-select-pane on setw -g mouse- ...

  5. 每天一道LeetCode--371. Sum of Two Integers

    alculate the sum of two integers a and b, but you are not allowed to use the operator + and -. Examp ...

  6. 人情世故&潜规则

    大凡成功的牛人,无一例外都明白这一点.他们读懂了社会的本质和人际交往的潜规则,知道对方需要什么,知道对方脑子里在想什么.你几乎看不见他奔波劳碌,但是在不动声色中,他就已经实现人生目标.他们成功的密码是 ...

  7. 【转载】分析商品日均销量(DMS)对促销商品选择的意义

    江苏省常州市信特超市有限公司副总经理高晓颖 随着中国零售业的进一步的开放,竞争日趋激烈,促销活动在日常经营中已经成为不可缺少的一部分,频繁的促销活动的开展,零售业经营管理者越来越觉得促销商品的选择难度 ...

  8. 《Cocos2d-x实战 JS卷 Cocos2d-JS开发》上线了

    感谢大家一直以来的支持! 各大商店均开始销售:京东:http://item.jd.com/11659698.html当当:http://product.dangdang.com/23659808.ht ...

  9. 20150528—html使用Jquery遍历text文本框的非空验证

    <script src="jquery-1.7.2.min.js" type="text/javascript"></script> & ...

  10. DOS删除服务

    启动服务:   net   start   服务名   停止服务:   net   stop     服务名   卸载服务:   服务名   -uninstall 安装服务:sc create ser ...