PHP 相当于 Objective-C 的 @“string”

发布于 2024-12-26 12:02:12 字数 363 浏览 2 评论 0原文

我正在为 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

椒妓 2025-01-02 12:02:12

正确的习语应该是这样的:

$string = new NSString("Hello, World!");

虽然您可能能够想出更简洁的东西,但这是最好的方法。为了清晰起见并遵循标准语言约定,一些额外的字符是值得的。

编辑:

根据您的评论,我建议创建一个 NS 命名空间,并向该命名空间添加任何单字母函数,这样您就可以执行以下操作:

$string = ns\s("Hello, World!");

您可以对 NSNumber 等执行相同的操作,而不会污染全局命名空间或破坏PHP 习惯用法。

The proper idiom would be something along the lines of:

$string = new NSString("Hello, World!");

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:

$string = ns\s("Hello, World!");

You could do the same for NSNumber, etc, without polluting the global namespace or breaking PHP idiom.

夜血缘 2025-01-02 12:02:12

在 Core Foundation 中,Apple 使用 CFSTR("constant string");,因此这种方法是有先例的……

In Core Foundation, Apple use CFSTR("constant string");, so there's precedent for that kind of approach…

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文