php中new date|php中new self()关键字的用法

更新时间:2019-12-02    来源:网页编辑器下载    手机版     字体:

【www.bbyears.com--网页编辑器下载】


php  new self() 一般在类内部使用,作用是对自身类实例化,下面给个实例讲解如何使用:

class phpernote{
 public function __construct(){
  echo "一聚教程网!";
 }
 public static function getInstance(){
  new self();
 }
}
 
phpernote::getInstance();

返回结果:

一聚教程网!

例子

//self是指向类的本身,只跟类有关,跟任何对象实例无关

class test_self{
    private static $first_count; //定义静态变量
    private $last_count;
    function __construct(){
        $this->last_count=++self::$first_count;//直接用self调用变量的值赋值给另一个变量
    }
    function __destruct(){}
    function print_self(){
        print($this->last_count);
    }
}

$abc=new test_self();//实例化对象
$abc->print_self();//1
echo "
";

总结,,self是指向当前类的指针意思就是指类的本身了,所以我们如果要调用自己的话就可以这new self来创建了

本文来源:http://www.bbyears.com/shipin/81846.html

热门标签

更多>>

本类排行