PHP Setters/Getters 和构造函数

标签 php constructor architecture setter getter

我一直在网上搜索这个,但我似乎无法找到足够清楚让我理解的东西。我在 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 还是通过构造函数来声明和访问我的私有(private)字段?
  2. 哪个是最佳做法?
  3. 我理解构造函数的用途(也许不是),但是如果我可以通过 setter 和 getter 声明和访问我的私有(private)字段,那么拥有构造函数的意义何在?

请注意...这是我第一次将 OOP 与 Web 开发和 PHP 结合使用,我正在尝试通过编写一些代码来“弄脏”我的手来学习,以便我理解 OOP 中的某些内容。请保持简单。

最佳答案

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

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

class Animal 
{
    private $name;

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

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

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

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
    }
}

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

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;
    }
}

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

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 Setters/Getters 和构造函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11740145/

相关文章:

php - Nginx 位置配置(子文件夹)

php - 如何使用 JQuery 格式化货币

php - 包含、扩展、使用、宏、嵌入 Twig 之间的区别

java - 描述您用于 Java Web 应用程序的架构?

java - 是否有任何工具可以检测代码中的架构和设计模式?

javascript - 创建 Node.js + socket.io 服务器/客户端,进行用户身份验证、发送/接收数据

Javascript继承构造函数

c++ - 具有继承的构造函数定义

c++ - C++ 构造函数中的字符串作为参数

c# - ApiController,Viewmodel和DTO的设计