开发者

Is there a way to call a class "New"? [closed]

开发者 https://www.devze.com 2023-04-10 18:16 出处:网络
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical andcannot be reasonably answered in its current form. For help clari
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reop开发者_JS百科ened, visit the help center. Closed 11 years ago.

Maybe a stupid question, but anyway...

Is there a way to call a class "New"?

Class New
{
    ..
}

Won't work of course, is there another way?


new is a reserved word in php. a class with name "new" is not valid php

http://www.php.net/manual/en/reserved.keywords.php


You cannot use (most of) PHPs keywords as identifier of classes, methods or functions. Just avoid them. However, New is a really bad classname anyway, because its anything, but not self-speaking.


No, the word "new" is a reserved keyword (in probably all modern languages). You can't name anything "new". See the documentation for more detail: http://www.php.net/manual/en/reserved.keywords.php


You can declare a class "New", but it requires some stupid workarounds to actually use it:

class Old { /* ... */ }
class_alias("Old", "New");

$New = "new";
$n = new $New;

That's the only way to circumvent the reserved keyword issue. (New and new are the same in PHP, as it is case-insensitive for bare identifiers.)

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号