PHP 中的 Setter/Getters、可见性 [ OOP ]
我正在重新格式化我的类(如下所示) - 我认为我犯了一个错误,在我的类中将所有内容设置为具有相同的可见性,而属性实际上应该是私有的,而 getter/setter 在大多数情况下应该是公共的。
为了获取一个属性,我只需执行 $path->propertyname ,但我注意到使用 setter/getter 更实用。 如果我要实现 getter,我应该为每个属性创建一个 getter 还是可以为整个类创建一个 getter? 例如,假设我想获取控制器名称...
public function getController( ) {
return $this->controller;
}
这将返回控制器私有属性? 是否有更通用的 getter/setter 甚至是为我获取和集的混合方法?
这是类结构(只是属性/方法):
class urlParser {
public static $url = '';
public static $controller = '';
public static $baseController = '';
public static $urls = '';
public static $template = '';
public static $captures = '';
public static $contentXML = '';
function __construct( $urls ) {
$this->urls = $urls;
$this->baseController = $urls['default'];
$this->get_url();
}
public function get_url() {
// sets following properties:
// url, template, controller, contentXML, baseController
}
}
I'm in the process of reformatting my class ( seen below ) - I think I made the mistake of setting everything with the same visibility in my class, whereas properties should really be private and getters/setters should be public in most cases.
To grab a property I just do $path->propertyname but I've noticed its more practical to have setters/getters. If I were to implement getters, should I make one for each property or can I make one for the entire class? So for example say I want to get the controller name...
public function getController( ) {
return $this->controller;
}
And that would return the controller private property? Is it common to have a more generic getter/setter or even a hybrid method that gets and sets for me?
Here's the class structure ( just properties/methods ):
class urlParser {
public static $url = '';
public static $controller = '';
public static $baseController = '';
public static $urls = '';
public static $template = '';
public static $captures = '';
public static $contentXML = '';
function __construct( $urls ) {
$this->urls = $urls;
$this->baseController = $urls['default'];
$this->get_url();
}
public function get_url() {
// sets following properties:
// url, template, controller, contentXML, baseController
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

发布评论
评论(3)
看看 php 中的重载: http://php.net/overload
有两个神奇的方法 __get()
和 __set()
可能完全符合您的要求
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
PHP 对象有一些很好的神奇方法(尽管我不知道)不要全部使用,因为这可能会造成混乱)。 您可能正在寻找的是 __get() 和 __set()。 如果您选择将数据字段设置为私有,您仍然需要重构所有代码(您应该这样做,这样您的其他代码就不会错误地访问字段。这样做有助于使您的代码变得更好、更简单) 不过,我通常会尝试为每个私有数据字段设置 getter 和 setter ,
因为您可以轻松地看到每个字段的任何验证,而且我更像是 Java 开发人员而不是 PHP 开发人员。
PHP objects have some nice magic methods (although I don't use all of them because it can get confusing). The one you may be looking for is __get() and __set(). You would still have to refactor all of your code if you choose to make your data fields private (which you should so that you don't have your other code accessing fields incorrectly. Doing it this way helps make your code a little better and easier to maintain.
Although, I usually try to have getters and setters for each private data field because you can easily see any validation for each field. Plus I'm more of a Java developer than a PHP developer.