Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save simeonwillbanks/338907 to your computer and use it in GitHub Desktop.
Save simeonwillbanks/338907 to your computer and use it in GitHub Desktop.
PHP: Property Accessors Example Person Version 2
<?php
class Person {
public $first_name;
public $last_name;
public function __construct($first_name, $last_name)
{
$this->first_name = $first_name;
$this->last_name = $last_name;
}
/**
* Property accessor
* @return string
*/
public function full_name()
{
return $this->first_name.' '.$this->last_name;
}
/**
* 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();
}
}
$me = new Person("Simeon", "Willbanks");
// So clean!
echo $me->full_name."\n";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment