php public,static,private,protected,final,const,abstract

public:权限是最大的,可以内部调用,实例调用等。

protected: 受保护类型,用于本类和继承类调用。

private: 私有类型,只有在本类中使用。

final:PHP 5:不被改,不被继承( php5新增了一个 final 关键字。如果父类中的方法被声明为 final,则子类无法覆盖该方法。如果一个类被声明为 final,则不能被继承 )

const :一旦赋值不能被改变

abstract:抽象类。(php6)

static:

protected $cache = 300;   // 缓存时间
const da = '1';
static $dg = '2';
private $dd = '3';
public $df = '4';


 public function info(){
        echo $this->cache;
        echo self::da;
        echo self::$dg;
        echo $this->dd;
        echo $this->df;

}

public function使用:公共方法。使用的时候需要实例化new 

<?php
$a = new Article()
eho $a->t()
?>

 public static function使用:公共静态方法,无需实例化直接调用

<?php
 
class e6 {
    //静态属性
    static public $n = 1;
    //静态方法
    static public function test() {
        echo 'hello';
    }
}
 
 
//访问静态元素
echo e6::$n;       //输出 1
echo e6::test();   // 输出 hello
 
 
//修改静态属性
e6::$n = 88;
echo e6::$n;       //输出88
 
?>

// thinkphp5代码

indexBase.php

View Code

index.php

<?php
namespace appindexcontroller;
use appindexcontrollerIndexBase;

//class index extends IndexBase{
class index{
    public function index(){
        echo IndexBase::$n;       //输出 1
        echo IndexBase::test();   // 输出 hello

        echo "</br>";

        //修改静态属性
        IndexBase::$n = 88;
        echo IndexBase::$n;       //输出88
    }


}

总结:static不用导入,就可以全局通用;静态属性可以被改变

 static使用

class IndexBase{
    static $s = 9; 
}

class index{
    public function index(){
        echo IndexBase::$s;   // 输出9
        IndexBase::$s = 89;
        echo IndexBase::$s;   // 输出89

    }


}

protected static:受保护的静态属性

1212

原文地址:https://www.cnblogs.com/wesky/p/7526151.html