为什么我会使用OOP来隐藏信息?

Aar*_*ron 4 php oop

为什么我要用PHP来隐藏信息?

<?php

function foo($bar) {
    echo $bar;
}

foo('foobar');

?>
Run Code Online (Sandbox Code Playgroud)

<?php

class foo {
    private $bar;

    function __construct($b){
        $this->bar = $b;
    }

    function display() {
        echo $this->bar;
    }
}

$foobar = new foo('bar');
$foobar->display();

?>
Run Code Online (Sandbox Code Playgroud)

900*_*000 7

无论你发布什么都成为你的公共合同.其他人开始依赖你的公共方法,使用它们并期望它们在那里并且工作相同.半年后,"其他人"也许就是你自己.

所以要公开宣传要非常保守.它使您可以自由地发展代码并修复其中的错误,而不会破坏公共接口.

  • "其他人"的+1可能是半年后的自己." Eagleson定律:你在六个月内没有看过的任何你自己的代码也可能是由其他人写的. (2认同)