我应该使用C#般在PHP财产处理? [英] Should I use C#-like property handling in PHP?
问题描述
嗨SO社区,
我在这里看到: HTTP://www.mustap。 COM / phpzone_post_203_setter和拼命三郎使用的财产处理常见的C#
A类型的参数,那就是:
。如果在C#中,你可以这样定义一个类:
公共类Person
{
私人字符串_name;
公共字符串名称
{
{返回_name; }
集合{_name =价值; }
}
}
那么,为什么是明智与否来定义在这样的PHP类似的属性:
公共类Person
{
私人变量$名称;
公共函数名()
{
如果(func_num_args()== 0){$返回这 - >名; }
,否则{$这个 - >名称= func_get_arg(0); }
}
}
海事组织,这并不在PHP看起来很干净。这就像你试图堵塞C#的方式进入PHP,因为你认为这是C#的一个很好的功能。
我把它看作是:在PHP中,你正在做单一的功能的做两个完全不同的东西,这是没有意义的。同时,在C#,它是单一的属性的,它被设计成一个单一访问点为对象来进行设置/检索。
Hey SO community, I saw here: http://www.mustap.com/phpzone_post_203_setter-and-getter an argument for using a type of property handling common to C#, that is: If in C#, you can define a class like this:
public class Person
{
private string _name;
public string Name
{
get{ return _name; }
set{ _name = value; }
}
}
then why is it wise or not to define a similar property in php like this:
public class Person
{
private var $name;
public function Name()
{
if(func_num_args() == 0) { return $this->name; }
else {$this->name = func_get_arg(0); }
}
}
IMO, that doesn't look clean in PHP. It's like you're trying to jam the C# way into PHP because you think it's a nice feature of C#.
I see it as: In PHP, you're making a single function for doing two completely different things, which doesn't make sense. Meanwhile, in C#, it is single property, which is designed to be a single point of access for an object to be set/retrieved.
这篇关于我应该使用C#般在PHP财产处理?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!