什么是php中的简单示例封装? [英] What is encapsulation with simple example in php?

查看:88
本文介绍了什么是php中的简单示例封装?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

什么是php中的简单示例封装?

What is encapsulation with simple example in php?

推荐答案

封装只是将一些数据包装在对象中.术语封装"通常与信息隐藏"互换使用. Wikipedia有一篇相当详尽的文章.

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.

以下是:

<?php

class App {
     private static $_user;

     public function User( ) {
          if( $this->_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();

?>

这篇关于什么是php中的简单示例封装?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆