PHP 中的 Setter/Getters、可见性 [ OOP ]

发布于 2024-08-01 13:35:39 字数 1057 浏览 2 评论 0原文

我正在重新格式化我的类(如下所示) - 我认为我犯了一个错误,在我的类中将所有内容设置为具有相同的可见性,而属性实际上应该是私有的,而 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 技术交流群。

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

发布评论

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

评论(3

も让我眼熟你 2024-08-08 13:35:39

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.

不顾 2024-08-08 13:35:39

看看 php 中的重载: http://php.net/overload

有两个神奇的方法 __get()__set() 可能完全符合您的要求

take a look at overloading in php: http://php.net/overload

there are two magic methods __get() and __set() which might do exactly what you want

冰火雁神 2024-08-08 13:35:39

在你的情况下拥有 setter 和 getter 是没有意义的。

如果您除了返回控制器之外还必须执行任何操作,则不需要 getter,因为如果您在设置属性后不对属性执行任何操作,则不需要 setter。

如果您确实需要 getter 和 setter,您可以对所有属性使用 __get 和 __set 魔术方法。 但如果情况像我上面描述的那样,那就完全没有意义了。

Having setters and getters in you case is pointless.

In case you have to do anything besides returning controller, you don't need getter as you don't need setter if you don't do anything with property after it has been set.

In case you really want getters and setters, you can use __get and __set magic methods for all your properties. But that is totally pointless if the case is as I described above.

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