获取php中设置的属性
我来自 C# 环境,在学校开始学习 PHP。 我习惯于像这样在 C# 中设置属性。
public int ID { get; set; }
php 中与此等效的是什么?
谢谢。
I'm from the C# environment and I'm starting to learn PHP in school.
I'm used to set my properties in C# like this.
public int ID { get; set; }
What's the equivalent to this in php?
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
没有,尽管有一些在未来版本中实现的建议。
不幸的是,现在您需要手动声明所有 getter 和 setter。
对于一些魔法(PHP 喜欢魔法),您可以查找
__set
和__get
魔法方法。示例
There is none, although there are some proposals for implementing that in future versions.
For now you unfortunately need to declare all getters and setters by hand.
for some magic (PHP likes magic), you can look up
__set
and__get
magic methods.Example
谢谢大家的回答。它帮助我创建了这样的东西:
在我的父类中:
ObjectHelper::existsMethod 是一个仅检查给定的受保护方法是否存在的方法。
所以我可以在任何课程中使用这样的东西:
我受到 C# 的启发:)
你们对此有何看法,伙计们?
如果有人想使用它,这是我的 ObjectHelper:
Thanks for your answers everyone. It helped me to create something like this:
In my parent class:
ObjectHelper::existsMethod is a method which just check if given protected method exists.
So I can use something like this in any class:
I was inspired by C# :)
What do you think about this, guys?
Here is my ObjectHelper if someone would like to use it:
Mchi 是正确的,但是还有另一种方法可以通过使用单个函数来做到这一点
但是是的,这种方法几乎与什么一致你用c#做的。但这只是一种替代方法
或者尝试在您的类中使用 php 的 __set 和 __get 更多信息请参见
http://php.net/manual/en/language.oop5.overloading.php
Mchi is right, but there is another way of doing it by using single function
But yeah this approach is pretty much inline with what you do in c#. but this is only an alternative
Or try using php's __set and __get in your class more info here
http://php.net/manual/en/language.oop5.overloading.php
另一个使用变量函数名称的示例
Another exampled using Variable function name
我知道我在这个问题上有点晚了,但我自己也有同样的问题/想法。作为一名使用 PHP 的 C# 开发人员,当工作需要时,我希望有一种简单的方法来创建我在 C# 中能够做到的属性。
今天下午我起草了一份初稿,它允许您创建支持字段并指定它们的访问器,或者使用没有支持字段的纯访问器。我将随着代码的发展更新我的答案,并在我将其达到可以作为作曲家包导入的状态时提供一个链接。
为简单起见,我将该功能创建为 PHP 特征,以便您可以将其放入您想要的任何类中,而不必扩展基类。最终,我希望扩展此功能以区分对属性的外部公共调用和受保护/私有调用。
这是特征本身的代码:
这是我使用 Codeception 格式创建的快速单元测试:
I know I am a bit late to the party on this question, but I had the same question/thought myself. As a C# developer who does PHP, when the job requires, I want to have a simple way to create properties just I would be able to in C#.
I whipped up a first draft this afternoon which allows you to create the backing fields and specify their accessors or have pure accessors with no backing field. I will update my answer as the code evolves and provide a link when I get it to the state where it can be imported as a composer package.
For simplicity, I created the functionality as a PHP trait so you can drop it in to any class you want instead of having to extend a base class. Eventually I hope to extend this functionality to discern between external public calls to the properties and protected/private calls.
Here is the code for the trait itself:
Here is a quick unit test I created using the Codeception format:
我喜欢使用这种模式:
I like to use this pattern:
从 2025 年的 PHP 8.4 开始,我们终于能够使用所谓的属性挂钩 (https:// /wiki.php.net/rfc/property-hooks)。
与 C# 代码等效的代码如下所示:
可以使用箭头表示法,如下所示:
如果您愿意,可以省略 getter 或 setter,然后它将被 PHP 的默认读/写行为替换。显然,这个简单的例子等同于:
创建虚拟属性也是可以的。他们不允许有二传手:
As of PHP 8.4 in 2025, we are finally able to use so-called property hooks (https://wiki.php.net/rfc/property-hooks).
Your equivalent to the C# code looks as follows:
It is possible to use the arrow notation as follows:
You can leave out the getter or the setter if you like, it will then be replaced by PHP's default read/write behavior. Obviously, this simple example is the same as:
It is also possible to create virtual properties. They are not allowed to have a setter:
这是 PHP ;你不需要设置
就可以了
this is PHP ; you don't need get set
will work