教程集 www.jiaochengji.com
教程集 >  脚本编程  >  php  >  正文 php中new self()关键字的用法

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

发布时间:2016-10-04   编辑:jiaochengji.com
教程集为您提供php中new self()关键字的用法等资源,欢迎您收藏本站,我们将为您提供最新的php中new self()关键字的用法资源
self()是用在类中了这个new self()用法小编第一次听说过了,不过看了一些相关文章 self()与this有那么一像但又不全像,下面我们来看看php中new self()关键字的用法.


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

<?php
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 '<br />';

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

您可能感兴趣的文章:
php中new self()关键字的用法
详解PHP的self关键字
PHP面向对象中new self( )和 new static( ) 的区别
PHP保留类及特殊类
了解PHP中self关键字的相关知识
关于PHP中self关键字的用法详解
分分钟搞定PHP的self关键字
PHP 的 new static 和 new self
解析PHP的self关键字
理解php5中static和const关键字用法

[关闭]
~ ~