看php手册2015-03-19版后备注

类与对象->基本概念:
1,#############################
::class
自 PHP 5.5 起,关键词 class 也可用于类名的解析。使用 ClassName::class 你可以获取一个字符串,包含了类 ClassName 的完全限定名称。这对使用了 命名空间 的类尤其有用。
Example #7 类名的解析as stated in the docs is:
<?php
namespace NS {
    class ClassName {
    }
    echo ClassName::class;
}
?>  
以上例程会输出:
NS\ClassName


2,#############################
Just to be clear: the correct way of validating a classname, as stated in the docs is:
$valid = preg_match('/^[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*$/', $className);

3,#############################
属性中的变量可以初始化,但是初始化的值必须是常数,这里的常数是指 PHP 脚本在编译阶段时就可以得到其值,而不依赖于运行时的信息才能求值
PHP 5.3.0 新增 支持Nowdoc声明类属性; 不包含变量的heredoc也是可以的,包含变量就错。


new static() 会遵循继承关系,new 的是子类
new self()  不会被继承,new 的是 self 这个词所在的那个类


###
As of PHP 5.6 you can finally define constant using math expressions, like this one:
<?php

class MyTimer {
     const SEC_PER_DAY = 60 * 60 * 24;
 }

?>

###
自 PHP 5.3.3 起,在命名空间中,与类名同名的方法不再作为构造函数。这一改变不影响不在命名空间中的类。
<?php
namespace Foo;
class Bar {
    public $a;
    public function Bar() {
          $this->a = 'to here';
    }
    public function getA(){
          return $this->a;    
    }
}
$bar = new Bar();
echo $bar->getA();  //空 ; 去掉命名空间则输出  to here;



###
自 PHP 5.3.0 起,可以通过变量来引用类,该变量的值不能是关键字(如 self,parent 和 static)。




带整理:
self,parent 和 static
 public(公有),protected(受保护)或 private(私有)

posted on 2015-06-01 22:47  珩~  阅读(289)  评论(0编辑  收藏  举报

导航