PHP 相当于 Objective-C 的 @“string”
我正在为 PHP 编写一个类库,它将在 Objective-C 中实现 Cocoa 框架的一些功能。
Objective-C 有一个方便的速记法来创建 NSString
对象:
NSString *string = @"Hello World!";
我正在尝试在 PHP 中找到最优雅的等效方法。
目前我已经采用:
$string = a("Hello World!");
a()
是一个创建适当对象的函数。
谁能建议一种更优雅的语法?
显然它必须是有效的 PHP 代码。
I'm writing a class library for PHP which will implement some of the features of the Cocoa framework in Objective-C.
Objective-C has a handy shorthand for creating NSString
objects :
NSString *string = @"Hello World!";
I'm trying to come up with the most elegant equivalent for this in PHP.
At the moment I've gone with:
$string = a("Hello World!");
a()
is a function which creates the appropriate object.
Can anyone suggest a more elegant syntax ?
It obviously has to be valid PHP code.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
正确的习语应该是这样的:
虽然您可能能够想出更简洁的东西,但这是最好的方法。为了清晰起见并遵循标准语言约定,一些额外的字符是值得的。
编辑:
根据您的评论,我建议创建一个 NS 命名空间,并向该命名空间添加任何单字母函数,这样您就可以执行以下操作:
您可以对 NSNumber 等执行相同的操作,而不会污染全局命名空间或破坏PHP 习惯用法。
The proper idiom would be something along the lines of:
While you might be able to come up with something more terse, this is the best way. A few extra characters for zounds of clarity and following standard language conventions is worth it.
Edit:
Based on your comment, I would recommend creating an NS namespace, and adding any single-letter functions to that namespace, so you could do the following:
You could do the same for NSNumber, etc, without polluting the global namespace or breaking PHP idiom.
在 Core Foundation 中,Apple 使用
CFSTR("constant string");
,因此这种方法是有先例的……In Core Foundation, Apple use
CFSTR("constant string");
, so there's precedent for that kind of approach…