dpwdldgn43486 2017-07-12 02:43
浏览 163

没有`new`关键字实例化PHP类?

I am used to the new keyword, so I was surprised to find that the following works for instantiation as well.

class MyClass
{
    const CONSTANT = 'constant value';

    function showConstant() {
        echo  self::CONSTANT;
    }
}

// $classname = new MyClass;
$classname = "MyClass";

echo $classname::CONSTANT; 

I can't seem to find any documentation pertaining to this online. Would someone help me out?

  • 写回答

3条回答 默认 最新

  • doulin9679 2017-07-12 02:45
    关注

    As of PHP 5.3.0, it's possible to reference the class using a variable [0]

    You're not creating an instance of an object. So you're not instantiating a class. PHP constants can be access statically.

    [0] http://php.net/manual/en/language.oop5.constants.php

    评论

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度