Created
April 24, 2014 00:14
-
-
Save souparno/11237024 to your computer and use it in GitHub Desktop.
Function Encaptulation in php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Encapsulation is just wrapping some data in an object. The term "encapsulation" is often used interchangeably with "information hiding". Wikipedia has a pretty through article. | |
Here's an example from the first link in a Google search for 'php encapsulation': | |
<?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(); | |
?> | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment