php 术语:getters 和 public 方法之间的区别?

发布于 2024-09-17 06:47:55 字数 215 浏览 8 评论 0原文

我的问题更多的是关于术语而不是技术细节(或者是吗?)。

类中的 getter 方法和 public 方法有什么区别?它们是同一的还是有区别?

我问这个问题是因为我正在努力学习最佳编码实践,而这个领域对我来说似乎是灰色的。我正在注释我的代码,注意到我有一个名为“Getters”的大部分和另一个名为“公共方法”的大部分,然后我就像......“有什么区别?!”。

谢谢!

My question is more about terminology then technicalities (or is it?).

What's the difference between a getter method and a public method in a class? Are they one in the same or is there a distinction between them?

I ask because I'm trying to learn best coding practices and this area seems grey to me. I was commenting my code out and noticed I had a big section called "Getters" and another big section called "Public Methods" and then I was like... "what's the diff?!".

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

避讳 2024-09-24 06:47:55

简单来说,PHP 中的 getter 只是一种允许代码的其他部分访问某个类属性的方法。

例如:

<?php

class Person {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    // Getter
    public function getName() {
        return $this->name;
    }
}

$bob = new Person('Bob');
echo $bob->getName(); // Bob

?>

方法不一定被设计为仅返回属性;您可以创建其他方法,以便您的类可以做一些时髦的事情

为了扩展上面的示例,让我们为 Person 类提供一个名为 say() 的方法,并为其提供一个代表要说的内容的函数/方法参数:

    public function say($what) {
        printf('%s says "%s"', $this->name, $what);
    }

然后调用它我们从类中创建一个对象:

$bob = new Person('Bob');
echo $bob->getName(), "\n"; // Bob
$bob->say('Hello!');        // Bob says "Hello!"

请注意,在 say() 方法中,我引用了 $this->name。没关系,因为 $name 属性是在同一个类中找到的。 getter(及其相应的 setter,如果有)的目的是允许代码的其他部分访问此属性。

In simple terms, a getter in PHP is simply a method that allows other parts of your code to access a certain class property.

For example:

<?php

class Person {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    // Getter
    public function getName() {
        return $this->name;
    }
}

$bob = new Person('Bob');
echo $bob->getName(); // Bob

?>

A method may not necessarily be designed to just return a property though; you can create other methods so that your class can do funky things.

To expand on the above example, let's give the Person class a method called say(), and give it a function/method parameter representing what to say:

    public function say($what) {
        printf('%s says "%s"', $this->name, $what);
    }

And call it after we create an object out of the class:

$bob = new Person('Bob');
echo $bob->getName(), "\n"; // Bob
$bob->say('Hello!');        // Bob says "Hello!"

Notice that inside the say() method I refer to $this->name. It's OK, since the $name property is found in the same class. The purpose of a getter (and its corresponding setter if any) is to allow other parts of your code to access this property.

泪眸﹌ 2024-09-24 06:47:55

Getter 是返回私有变量值的公共方法。同样,setter 是允许修改或“设置”私有变量的公共方法。

Getters are public methods that return the value of a private variable. In a similar vein, setters are public methods that allow modification or "setting" of a private variable.

弄潮 2024-09-24 06:47:55

公共方法可以从类外部更新,并且类不一定需要知道它。

公共 getter 或 setter 为您提供了更大的灵活性 - 即,当我们尝试读取 $obj->$property 时,变量可能尚未准备好。但是,如果我们使用 $obj->getSomething() 我们可以对该变量执行任何操作,使其准备就绪,

不同之处在于公共 getter 通常返回一个私有变量。这意味着从对象获取属性状态的唯一方法是通过方法获取它,该方法可能会也可能不会做额外的事情。

Public methods can be updated from outside the class, and the class doesn't necessarily have to know about it.

A public getter or setter gives you more flexibility - i.e. when we try and read $obj->$property, the variable might not be ready. However, if we use $obj->getSomething() we can do anything to that variable, to make it ready,

The difference is that public getters generally return a private variable. This means the only way to get the state of a property from the object is to get it via a method, which may or may not do extra things.

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