在php中用简单的例子封装是什么?
发布于 2009-06-12 06:49:03
封装只是将一些数据包装在一个对象中。术语“封装”通常与“信息隐藏”互换使用。维基百科有a pretty thorough article。
以下是来自Google search for 'php encapsulation'中的the first link的示例
<?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();
?>https://stackoverflow.com/questions/985298
复制相似问题