Can a namespace start with a number in PHP?

浪尽此生 提交于 2020-01-11 06:15:12

问题


When declaring the following namespace:

<?php

namespace Example\3000;

I got this error:

Parse error:  syntax error, unexpected '3000' (T_LNUMBER), expecting identifier (T_STRING) in [...]

So I wondered whether a namespace in PHP may start with a number?


回答1:


No, it must not. It must start with a letter.

It took me a while to find this in a comment on PHP.net.

To use numbers e.g. for versioning it is necessary to prepend letters, e.g. like in the following:

<?php

namespace Example\V_3000;



回答2:


Note: Special character cannot be used. The only special character I have found that works is the underscore (_). For example if you want a folder for version 3.0.01, then do the following:

    <?php
    namespace Example\v3_0_01;


来源:https://stackoverflow.com/questions/33875380/can-a-namespace-start-with-a-number-in-php

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!