对象和数据结构

收藏696

阅读33253

更新时间2022-04-13

对象和数据结构 Objects and Data Structures

1. 使用 getters 和 setters Use object encapsulation

2. 给对象使用私有或受保护的成员变量

1. 使用 getters 和 setters

在PHP中你可以对方法使用public, protected, private 来控制对象属性的变更。

  1. 当你想对对象属性做获取之外的操作时,你不需要在代码中去寻找并修改每一个该属性访问方法

  2. 当有set对应的属性方法时,易于增加参数的验证

  3. 封装内部的表示

  4. 使用setget时,易于增加日志和错误控制

  5. 继承当前类时,可以复写默认的方法功能

  6. 当对象属性是从远端服务器获取时,get*set*易于使用延迟加载

此外,这样的方式也符合OOP开发中的开闭原则

坏:

 class BankAccount
{
    public $balance = 1000;
}
 
$bankAccount = new BankAccount();
 
// Buy shoes...
$bankAccount->balance -= 100;

好:

 class BankAccount
{
    private $balance;
 
    public function __construct(int $balance = 1000)
    {
      $this->balance = $balance;
    }
 
    public function withdraw(int $amount): void
    {
        if ($amount > $this->balance) {
            throw new \Exception('Amount greater than available balance.');
        }
 
        $this->balance -= $amount;
    }
 
    public function deposit(int $amount): void
    {
        $this->balance += $amount;
    }
 
    public function getBalance(): int
    {
        return $this->balance;
    }
}
 
$bankAccount = new BankAccount();
 
// Buy shoes...
$bankAccount->withdraw($shoesPrice);
 
// Get balance
$balance = $bankAccount->getBalance();

2. 给对象使用私有或受保护的成员变量

  • 对public方法和属性进行修改非常危险,因为外部代码容易依赖他,而你没办法控制。对之修改影响所有这个类的使用者。 public methods and properties are most dangerous for changes, because some outside code may easily rely on them and you can't control what code relies on them. Modifications in class are dangerous for all users of class.

  • 对protected的修改跟对public修改差不多危险,因为他们对子类可用,他俩的唯一区别就是可调用的位置不一样,对之修改影响所有集成这个类的地方。 protected modifier are as dangerous as public, because they are available in scope of any child class. This effectively means that difference between public and protected is only in access mechanism, but encapsulation guarantee remains the same. Modifications in class are dangerous for all descendant classes.

  • 对private的修改保证了这部分代码只会影响当前类private modifier guarantees that code is dangerous to modify only in boundaries of single class (you are safe for modifications and you won't have Jenga effect).

所以,当你需要控制类里的代码可以被访问时才用public/protected,其他时候都用private

可以读一读这篇 博客文章 ,Fabien Potencier写的.

坏:

 class Employee
{
    public $name;
 
    public function __construct(string $name)
    {
        $this->name = $name;
    }
}
 
$employee = new Employee('John Doe');
echo 'Employee name: '.$employee->name; // Employee name: John Doe

好:

 class Employee
{
    private $name;
 
    public function __construct(string $name)
    {
        $this->name = $name;
    }
 
    public function getName(): string
    {
        return $this->name;
    }
}
 
$employee = new Employee('John Doe');
echo 'Employee name: '.$employee->getName(); // Employee name: John Doe

科技资讯

更多

精选课程

更多
前端入门_HTML5
前端入门_HTML5

共29课时

61.7万人学习

CSS视频教程-玉女心经版
CSS视频教程-玉女心经版

共25课时

39.3万人学习

JavaScript极速入门_玉女心经系列
JavaScript极速入门_玉女心经系列

共43课时

71万人学习

独孤九贱(1)_HTML5视频教程
独孤九贱(1)_HTML5视频教程

共25课时

61.6万人学习

独孤九贱(2)_CSS视频教程
独孤九贱(2)_CSS视频教程

共22课时

23万人学习

独孤九贱(3)_JavaScript视频教程
独孤九贱(3)_JavaScript视频教程

共28课时

33.9万人学习

独孤九贱(4)_PHP视频教程
独孤九贱(4)_PHP视频教程

共89课时

125万人学习

关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号