Return to Snippet

Revision: 21744
at December 19, 2009 18:25 by Alexw


Initial Code
<?php

	class Person
	{
		private $first;
		private $last;
		
		public setName ($name)
		{
		
		}
		
		public getName ($name)
		{
		
		}
		
		public function __set ($name, $value)
		{
			$key = strtoupper(substr($name, 1)) . strtolower(substr($name, 0, 1));
			$setter = 'set' . $key;
			
			if (method_exists($this, $setter)) {
				$this->$setter($value);
				return $value;
			}
			
			trigger_error('Undefined property via ' . $name . ' in ' . $trace[0]['file'] . ' on line ' . $trace[0]['line'], E_USER_NOTICE);
		}
		
		public function __get ($name)
		{
			$key = strtoupper(substr($name, 1)) . strtolower(substr($name, 0, 1));
			$getter = 'set' . $key;
			
			if (method_exists($this, $getter)) {
				return $this->$getter();
			}
			
			trigger_error('Undefined property via ' . $name . ' in ' . $trace[0]['file'] . ' on line ' . $trace[0]['line'], E_USER_NOTICE);
		}
	}
	
?>

Initial URL

                                

Initial Description
OOP style getters and setters in PHP.

Initial Title
PHP Getters and Setters

Initial Tags
php

Initial Language
PHP