PHP Setter/Getter 和构造函数 [英] PHP Setters/Getters and Constructor

查看:27
本文介绍了PHP Setter/Getter 和构造函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在网上搜索这个,但我似乎无法找到足够清楚让我理解的东西.我在 Java 中看到了关于这里的类似"问题.

I have been searching for this online, but I can't seem to find something that is clear enough for me to understand. I have seen "similiar" questions on here about this in Java.

class animal{
    private $name;

    // traditional setters and getters
    public function setName($name){
        $this->name = $name;
    }

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

    // animal constructors
    function __construct(){
       // some code here
    }

    // vs

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

$dog = new animal();
$dog->setName("spot");
echo $dog->getName();

// vs

$dog = new animal("spot");

  1. 我应该通过 setter 和 getter 还是通过构造函数声明和访问我的私有字段?
  2. 哪个是最佳实践?
  3. 我了解构造函数的用途(也许不是),但是如果我可以通过 setter 和 getter 声明和访问我的私有字段,那么拥有构造函数的意义何在?

请注意...这是我第一次在 Web 开发和 PHP 中使用 OOP,我试图通过编写一些代码来让我的手脏"来学习,以便我理解 OOP 中的某些东西.请保持简单.

Please note...this is my first time using OOP with web development and PHP, and I'm trying to learn by getting my hands "dirty" by writing some code in order for me to understand certain things in OOP. Please keep it simple.

推荐答案

与其说是最佳实践,不如说是语义问题.

It is more a matter of semantics than best practice per say.

在您的示例中,您的业务逻辑可能会确定动物总是需要一个名字.所以用名字构造对象是有意义的.如果你不想让动物的名字要改,那你就不用写二传手了.

In your example, your buisness logic may determine that an animal always needs a name. So it makes sense to construct the object with a name. If you do not want to allow an animal's name to be changed, then you don't write a setter.

class Animal 
{
    private $name;

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

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

您可能拥有动物不必拥有的其他属性,例如主人你只为 i.e. 编写一个 getter/setter

You may have other properties that an animal doesn't have to have, like an owner that you only write a getter/setter for i.e.

class Animal
{
    private $name;
    private $owner;

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

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

    public function setOwner($owner)
    {
        $this->owner = $owner
    }
}

但是如果你发现你总是同时与一个主人一起创造一个动物为方便起见,您可能希望将其放在构造函数签名中

But if you find that you are always creating an animal with an owner at the same time you may want to put that in the contructor signature for convenience

class Animal
{
    private $name;
    private $owner;

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

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

    public function setOwner(Owner $owner)
    {
        $this->owner = $owner
    }

    public function getOwner()
    {
        return $this->owner;
    }
}

如果所有者是您应用程序中的另一个类,您可以键入提示您的构造函数需要特定类型(类)的所有者.所有这些都是为了让您或其他开发人员更容易理解您的代码背后的一些要求/逻辑 - 以及可能在这里或那里捕获错误

If the owner is another class in your application, you can type hint that your constructor needs an owner of a specific type (class). All of this is used to make it easier for you, or another developer to understand some of the requirements/logic behind your code - as well as potentially catching a bug here or there

class Owner 
{
    private $name;

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

class Animal
{
    private $name;
    private $owner;

    public function __construct($name, Owner $owner = null)
    {    
        $this->name = $name;
        $this->owner = $owner;
    }

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

    public function setOwner(Owner $owner)
    {
        $this->owner = $owner
    }

    public function getOwner()
    {
        return $this->owner;
    }
}

// Create a new owner!
$dave = new Owner('Farmer Dave');

// a standard php empty object
$otherObj = new \stdClass();

// Create a new animal
$daisy = new Animal('Daisy');

// Farmer dave owns Daisy
$daisy->setOwner($dave);

// Throws an error, because this isn't an instance of Owner
$daisy->setOwner($otherObj);

// Set up Maude, with Dave as the owner, a bit less code than before!
$maude = new Animal('Maude', $dave);

这篇关于PHP Setter/Getter 和构造函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆