关于php权限

public: 公有类型

        在子类中可以通过self::var调用public方法或属性,parent::method调用父类方法

      在实例中可以能过$obj->var 来调用 public类型的方法或属性

protected: 受保护类型
        在子类中可以通过self::var调用protected方法或属性,parent::method调用父类方法

        在实例中不能通过$obj->var 来调用  protected类型的方法或属性

private: 私有类型
 该类型的属性或方法只能在该类中使用,在该类的实例、子类中、子类的实例中都不能调用私有类型的属性和方法


私有属性和方法不会继承 protected会继承

两者都不能在外部被直接访问 


只能在类的内部使用->不可以在实例化的xx 直接 xx->某private protected属性的东西 这样调用


this就是指向当前对象实例的指针,不指向任何其他对象或类

self是指向类本身,也就是self是不指向任何已经实例化的对象 so一般self使用来指向类中的静态变量

parent是指向父类的指针 多数用于调用父类的构造方法

即使方法被重载 也可以用parent::调用 

final function不可以被重载 

final class不能被继承

没有多重继承 但是可以实现多个接口并继承一个类


class xx{

const aa = bb;

static function heh(){

}

}

xx:aa直接访问常量 不需初始化

xx::heh();

静态方法中不能有this 因为可能没有实例化的对象


通常 从类的外部直接访问类的属性是糟糕的 所以最好进行封装

留空的权限等于public


self 和 parent 的区别给
  a).在子类中常用到这两个对像。他们的主要区别在于self可以调用父类中的公有或受保护的属性,但parent不可以调用

  b).self:: 它表示当前类的静态成员(方法和属性) 与 $this 不同

 $this是指当前对像(即已实例化)对象 所以调用方法不能使用

另外MVC框架中的各个class都是使用过程都是实例化对象

所以使用方法参数时 权限控制都需要注意

<?php
class xx{


public $test=0;
public function __construct(){
$test =1;
}
public function index(){
echo "1111";


}
private function index1(){
echo "2222";
$test=2;
}
protected function index2(){
//$this->index1();
echo "33333";




}


function index3(){
echo "44444";
}
public function test(){
$this->index();//正常
$this->index3();//正常

$this->index1();//正常
self::index2();//正常
//$this>index2();//error
}
final function index4(){
echo "66666";
}

}
class bb extends xx{
public function __call($name,$arguments){
if($name == "index"){
echo "test is ok";
}else{
echo "eeeee";
}
}
public function __construct(){
echo "<br/>";
echo "<br/>";
self::index();//正常
//self::index1(); error
self::index2();//正常
self::index3();//正常
echo "<br/>";
parent::index();//正常
//parent::index1(); //error
parent::index2();//正常
parent::index3();//正常
parent::index4();




echo "<br/>";
$this->index();
//$this>index1();error
//$this>index2(); error
//$this>index3();error
/*parent::$test=3;*/


}


}


$s = new xx();
$s->index3();//正常
//$s->index2();//error
//$s->index1();error


echo "<br/>";
$s->test();


$b = new bb();
echo "<br/>";
$b->index3();
echo "<br/>";
$b->index();






评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值