What is encapsulation with simple example in php?

前端 未结 14 1498
谎友^
谎友^ 2020-12-08 02:27

What is encapsulation with simple example in php?

14条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-08 02:57

    Encapsulation is just wrapping some data in an object. The term "encapsulation" is often used interchangeably with "information hiding". Wikipedia has a pretty thorough article.

    Here's an example from the first link in a Google search for 'php encapsulation':

    _user == null ) {
                   $this->_user = new User();
              }
              return $this->_user;
         }
    
    }
    
    class User {
         private $_name;
    
         public function __construct() {
              $this->_name = "Joseph Crawford Jr.";
         }
    
         public function GetName() {
              return $this->_name;
         }
    }
    
    $app = new App();
    
    echo $app->User()->GetName();
    
    ?>
    

提交回复
热议问题