doujiu4643 2015-04-10 12:37
浏览 26

有哪些新方法可以创建新对象?

I am using the NetBeans editor. In the following code, there is no error, but I am confused about something: why do we return new static?

class test {
     static public function getnew(){
          return new static;
     }
}
class child extends test {}

$obj1 = new test ();
$obj2 = new $obj1;
var_dump($obj1!==$obj2);
$obj3 = test::getnew();
var_dump($obj3 instanceof test);
$obj4 = child::getnew();
var_dump($obj4 instanceof child );

The resulting output:

boolean true  
boolean true  
boolean true

So what is the return new static doing here?

  • 写回答

2条回答 默认 最新

  • duandan4680 2015-04-10 13:40
    关注
    return new static;
    

    is instantiating the class "test" with late static binding. So when you extend the class "test" this will give you an instance of the extended class. Very handy for using it as a static factory method.

    评论

报告相同问题?

悬赏问题

  • ¥15 MATLAB动图问题
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题