Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why would I use OOP to hide information?

Tags:

oop

php

Why would I use PHP to Hide Information?

<?php

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

foo('foobar');

?>

VERSUS

<?php

class foo {
    private $bar;

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

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

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

?>
like image 779
Aaron Avatar asked Dec 02 '22 03:12

Aaron


1 Answers

Whatever you publish becomes your public contract. Other people start to depend on your public methods, use them and expect them to be there and work the same. 'Other people' maybe yourself half a year later.

So be extremely conservative in what to make public. It gives you the freedom to evolve your code and fix bugs in it without breaking the public interface.

like image 54
9000 Avatar answered Dec 18 '22 04:12

9000