Created
March 20, 2010 21:05
-
-
Save simeonwillbanks/338909 to your computer and use it in GitHub Desktop.
PHP: Property Accessors Example Person
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
<?php | |
class Person { | |
public $first_name; | |
public $last_name; | |
public $address; | |
public $city; | |
public $state; | |
public $zip; | |
// Pseudo property accessors | |
public function full_name() {} | |
public function full_address() {} | |
/** | |
* Check for property accessor - call when exists | |
* @var string property name | |
* @return mixed | |
*/ | |
public function __get($property) | |
{ | |
// A property accessor exists | |
if (method_exists($this, $property)) | |
return $this->$property(); | |
} | |
} | |
$person = new Person; | |
// So clean! | |
echo $person->full_name."\n"; | |
echo $person->full_address."\n"; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment