私有变量的优点/缺点?
我习惯于将几乎所有类变量设为私有,并创建获取/设置它们的“包装”函数:
class Something{
private $var;
function getVar(){
$return $this->var;
}
}
$smth = new Something();
echo $smth->getVar();
我看到很多人都这样做,所以我最终也做了同样的事情:)
使用有什么优势吗他们这样与:
class Something{
public $var;
}
$smth = new Something();
echp $smth->var;
?
我知道私有意味着你不能直接在类之外访问它们,但对我来说,如果可以从任何地方访问变量似乎并不很重要......
那么私有变量是否还有我缺少的任何其他隐藏优势?
I'm used to make pretty much all of my class variables private and create "wrapper" functions that get / set them:
class Something{
private $var;
function getVar(){
$return $this->var;
}
}
$smth = new Something();
echo $smth->getVar();
I see that a lot of people do this, so I ended up doing the same :)
Is there any advantage using them this way versus:
class Something{
public $var;
}
$smth = new Something();
echp $smth->var;
?
I know that private means that you can't access them directly outside the class, but for me it doesn't seem very important if the variable is accessible from anywhere...
So is there any other hidden advantage that I missing with private variables?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
它被称为封装,这是一件非常好的事情。封装将您的类与其他类的内部结构隔离开来,它们只能通过您的方法获得您允许的访问权限。它还可以保护它们免受您可能对类内部所做的更改的影响。如果没有封装,如果更改变量的名称或用法,该更改将传播到使用该变量的所有其他类。如果您强迫他们执行某个方法,那么您至少有机会能够处理该方法中的更改并保护其他类免受更改。
It's called encapsulation and it's a very good thing. Encapsulation insulates your class from other classes mucking about with it's internals, they only get the access that you allow through your methods. It also protects them from changes that you may make to the class internals. Without encapsulation if you make a change to a variable's name or usage, that change propagates to all other classes that use the variable. If you force them to go through a method, there's at least a chance that you'll be able to handle the change in the method and protect the other classes from the change.
如果您想将私有变量与公共 getter 和 setter 一起使用,或者您只想直接将变量声明为公共变量,则情况会有所不同。
使用“getters”和“setters”可能是好的原因是如果您想控制某人何时访问数据。
举个例子,假设您得到了这个:
然后您可以确保传递给该设置器的日期是有效的生日。
将变量声明为公共变量,则不能
但是,如果您只是像这样基于评论
这意味着,如果内部存储机制更改为 1970 年 1 月 1 日起的秒数,则您无需更改“外部 API”。原因是因为你可以完全控制它:
It differs from case to case if you want to use private variables with public getters and setters or if you just want to declare a variable as public directly.
The reason it might be good to use "getters" and "setters" is if you want to have control over when someone accessess the data.
As an example, lets say you got this:
Then you can make sure that the date passed in to that setter is a valid birthdate.
But you can't if you just declare the variable as public like this
Based on comments.
This means that if the internal storage mechanism would change to numbers of seconds from 1/1/1970 then you don't have to change the 'External API'. The reason is because you have full control over it:
访问修饰符在脚本语言中没有多大意义。许多实际的面向对象语言(例如 Python 或 Javascript)没有它们。
简单的 getter/setter 方法的流行仅仅是因为 PHP 没有为此提供显式的构造。 http://berryllium.nl/2011/02/ getters-and-setters-evil-or-necessary-evil/
Access modifiers don't make a whole lot of sense in scripting languages. Many actual object-oriented languages like Python or Javascript don't have them.
And the prevalence of naive getter/setter methods is simply due to PHP not providing an explicit construct for that. http://berryllium.nl/2011/02/getters-and-setters-evil-or-necessary-evil/
它将类实现的内部变量与用于外部更改的变量区分开来。还有受保护的变量,它们供内部使用,并由类的扩展使用。
我们将变量设为
私有
,这样只有类内的代码才能修改变量,从而防止外部干扰,保证变量的控制和预期行为。It is to demark variables that are internal to the implementation of the class, from variables that are intended for external change. There are also
protected
variables, which are for internal use, and use by extensions to the class.We make the variables
private
so that only the code within the class can modify the variables, protecting interference from outside, guaranteeing control and expected behaviour of the variable.封装的目的是向其他对象隐藏一个对象的内部结构。这个想法是,对象的外部足迹构成了它的定义类型,将其视为与其他对象的契约。在内部,它可能必须跳过一些环节才能提供面向外的功能,但这与其他对象无关。他们不应该惹事。
例如,假设您有一个提供销售税计算的类。基本上是某种公用事业服务对象。它有一些提供必要功能的方法。
在内部,该类正在访问数据库以获取一些值(例如给定管辖区的税),以便执行计算。它可能在内部维护数据库连接和其他与数据库相关的事情,但其他类不需要知道这一点。其他类只关心面向外部的功能契约。
假设稍后需要用外部 Web 服务替换数据库。 (该公司正在提供计算销售税的服务,而不是在内部维护它。)。由于该类是封装的,因此您可以轻松更改其内部实现以使用服务而不是数据库。该类只需要继续提供相同的面向外部的功能。
如果其他类对类的内部进行了修改,那么重新实现它就会冒破坏系统其他部分的风险。
The purpose of encapsulation is to hide the internals of an object from other objects. The idea is that the external footprint of the object constitutes it's defined type, think of it like a contract with other objects. Internally, it may have to jump through some hoops to provide the outward-facing functionality, but that's of no concern to other objects. They shouldn't be able to mess with it.
For example, let's say you have a class which provides calculations for sales tax. Some kind of utility service object, basically. It has a handful of methods which provide the necessary functionality.
Internally, that class is hitting a database to get some values (tax for a given jurisdiction, for example) in order to perform the calculations. It may be maintaining a database connection and other database-related things internally, but other classes don't need to know about that. Other classes are concerned only with the outward facing contract of functionality.
Suppose sometime later the database needs to be replaced with an external web service. (The company is going with a service for calculating sales tax rather than maintain it internally.). Because the class is encapsulated, you can change its internal implementation to use the service instead of the database very easily. The class just needs to continue to provide the same outward facing functionality.
If other classes were mucking around with the internals of the class, then re-implementing it would risk breaking others parts of the system.