<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	>

<channel>
	<title>Juraj Seffer &#124; Playground &#187; PHP</title>
	<atom:link href="http://jurajsplayground.com/category/php/feed/" rel="self" type="application/rss+xml" />
	<link>http://jurajsplayground.com</link>
	<description>Web development trinkets, articles about LAMP development, productivity tools and every day struggles</description>
	<pubDate>Sun, 20 May 2012 17:46:05 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.7</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>PHP Magic Methods - enforce defining of class properties</title>
		<link>http://jurajsplayground.com/2009/04/10/php-magic-methods-enforce-defining-of-class-properties/</link>
		<comments>http://jurajsplayground.com/2009/04/10/php-magic-methods-enforce-defining-of-class-properties/#comments</comments>
		<pubDate>Fri, 10 Apr 2009 16:53:23 +0000</pubDate>
		<dc:creator>Juraj</dc:creator>
		
		<category><![CDATA[All]]></category>

		<category><![CDATA[PHP]]></category>

		<category><![CDATA[getter]]></category>

		<category><![CDATA[magic methods]]></category>

		<category><![CDATA[objects]]></category>

		<category><![CDATA[setter]]></category>

		<guid isPermaLink="false">http://www.jurajsplayground.com/blog/?p=68</guid>
		<description><![CDATA[Sometimes it&#8217;s desirable to have a control over which properties can be assigned to an object. Typo mistakes, duplicating of property names, among others, are the commons pitfalls of PHP&#8217;s ability to set/get properties without defining them in a class.
The MagicMethods class enforces that class properties are defined in a class before we can set [...]]]></description>
			<content:encoded><![CDATA[<p>Sometimes it&#8217;s desirable to have a control over which properties can be assigned to an object. Typo mistakes, duplicating of property names, among others, are the commons pitfalls of PHP&#8217;s ability to set/get properties without defining them in a class.</p>
<p>The MagicMethods class enforces that class properties are defined in a class before we can set or get their values. Any class that extends this class will benefit from this behavior (unless magic method get overridden in the exteding class). If a getter or setter method is needed to manipulate a property, MagicMethod class will use these instead of directly accessing the value.</p>
<p>This functionality is especially handy for value objects or config classes.</p>
<p>Example:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">class</span> ConfigMagic <span style="color: #000000; font-weight: bold;">extends</span> MagicMethods_Abstract
<span style="color: #009900;">&#123;</span>
  <span style="color: #666666; font-style: italic;">// note $name is protected, not private, so that MagicMethods_Abstract can access it</span>
  protected <span style="color: #000088;">$name</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> getName<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
  <span style="color: #009900;">&#123;</span>
     <span style="color: #666666; font-style: italic;">// provides more complex way of getting a variable</span>
     <span style="color: #b1b100;">return</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>name<span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> setName<span style="color: #009900;">&#40;</span><span style="color: #000088;">$value</span><span style="color: #009900;">&#41;</span>
  <span style="color: #009900;">&#123;</span>
    <span style="color: #666666; font-style: italic;">// provides more complex way of setting a variable</span>
   <span style="color: #000088;">$this</span><span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>name <span style="color: #339933;">=</span> <span style="color: #000088;">$value</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000088;">$config</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> ConfigMagic<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$config</span><span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>name <span style="color: #339933;">=</span> <span style="color: #0000ff;">'magic'</span><span style="color: #339933;">;</span>
<span style="color: #990000;">var_dump</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$config</span><span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>name<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// string 'magic' (length=5)</span>
<span style="color: #990000;">var_dump</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">isset</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$config</span><span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>name<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>  <span style="color: #666666; font-style: italic;">// true</span>
<span style="color: #990000;">unset</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$config</span><span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>name<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">var_dump</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">isset</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$config</span><span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>name<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// false</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Exception: Cannot set property 'nonExisting'. Not defined in class ConfigMagic ...</span>
<span style="color: #000088;">$config</span><span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>nonExisting <span style="color: #339933;">=</span> <span style="color: #0000ff;">'magic'</span><span style="color: #339933;">;</span></pre></div></div>

<p>Download the files:<br />
<a href="http://www.jurajsplayground.com/wp-content/uploads/2009/04/magicmethods_abstract.php.txt">MagicMethods_Abstract.php</a><br />
<a href="http://www.jurajsplayground.com/wp-content/uploads/2009/04/magicmethodsexception.php.txt">MagicMethodsException.php</a></p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"> <span style="color: #009933; font-style: italic;">/**
 * Magic Methods
 * Provides magic methods for other classes, enforces property names.
 * Undefined property name throws an exception
 *
 * @package DAO
 * @uses MagicMethodsException
 * @example MyOwnClass extends MagicMethods_Abstract
 * @license Free to use and modify (including commercial use)
 * @see http://php.net/manual/en/language.oop5.magic.php
 * @author Juraj Seffer 
 * @copyright 2009, Juraj Seffer
 * @version $Id$
 * @access public
 */</span>
abstract <span style="color: #000000; font-weight: bold;">class</span> MagicMethods_Abstract
<span style="color: #009900;">&#123;</span>
  <span style="color: #009933; font-style: italic;">/**
   * Magic getter
   *
   * @param string $property
   * @access protected
   * @return mixed
   */</span>
  protected <span style="color: #000000; font-weight: bold;">function</span> __get<span style="color: #009900;">&#40;</span><span style="color: #000088;">$property</span><span style="color: #009900;">&#41;</span>
  <span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$getterMethod</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;get&quot;</span> <span style="color: #339933;">.</span> <span style="color: #990000;">strtoupper</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$property</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">method_exists</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">,</span> <span style="color: #000088;">$getterMethod</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      <span style="color: #b1b100;">return</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span><span style="color: #000088;">$getterMethod</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #b1b100;">else</span> <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span>property_exists<span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">,</span> <span style="color: #000088;">$property</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      throw <span style="color: #000000; font-weight: bold;">new</span> MagicMethodsException<span style="color: #009900;">&#40;</span>
        <span style="color: #0000ff;">&quot;Cannot get property '<span style="color: #006699; font-weight: bold;">$property</span>'. Not defined in class &quot;</span> <span style="color: #339933;">.</span> <span style="color: #990000;">get_class</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #009900;">&#41;</span>
      <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span>
      <span style="color: #b1b100;">return</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span><span style="color: #000088;">$property</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #009933; font-style: italic;">/**
   * Magic setter
   *
   * @param string $property
   * @param mixed $value
   * @access protected
   * @return void
   */</span>
  protected <span style="color: #000000; font-weight: bold;">function</span> __set<span style="color: #009900;">&#40;</span><span style="color: #000088;">$property</span><span style="color: #339933;">,</span> <span style="color: #000088;">$value</span><span style="color: #009900;">&#41;</span>
  <span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$setterMethod</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;set&quot;</span> <span style="color: #339933;">.</span> <span style="color: #990000;">strtoupper</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$property</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">method_exists</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">,</span> <span style="color: #000088;">$setterMethod</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      <span style="color: #b1b100;">return</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span><span style="color: #000088;">$setterMethod</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$value</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #b1b100;">else</span> <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span>property_exists<span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">,</span> <span style="color: #000088;">$property</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      throw <span style="color: #000000; font-weight: bold;">new</span> MagicMethodsException<span style="color: #009900;">&#40;</span>
        <span style="color: #0000ff;">&quot;Cannot set property '<span style="color: #006699; font-weight: bold;">$property</span>'. Not defined in class &quot;</span> <span style="color: #339933;">.</span> <span style="color: #990000;">get_class</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #009900;">&#41;</span>
      <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span>
      <span style="color: #000088;">$this</span><span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span><span style="color: #000088;">$property</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$value</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #009933; font-style: italic;">/**
   * Magic isset
   *
   * @param string $property
   * @access protected
   * @return mixed
   */</span>
  protected <span style="color: #000000; font-weight: bold;">function</span> __isset<span style="color: #009900;">&#40;</span><span style="color: #000088;">$property</span><span style="color: #009900;">&#41;</span>
  <span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span>property_exists<span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">,</span> <span style="color: #000088;">$property</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      throw <span style="color: #000000; font-weight: bold;">new</span> MagicMethodsException<span style="color: #009900;">&#40;</span>
        <span style="color: #0000ff;">&quot;Property '<span style="color: #006699; font-weight: bold;">$property</span>' not defined in class &quot;</span> <span style="color: #339933;">.</span> <span style="color: #990000;">get_class</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #009900;">&#41;</span>
      <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span>
      <span style="color: #b1b100;">return</span> <span style="color: #990000;">isset</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span><span style="color: #000088;">$property</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #009933; font-style: italic;">/**
   * Magic unset
   *
   * @param string $property
   * @access protected
   * @return void
   */</span>
  protected <span style="color: #000000; font-weight: bold;">function</span> __unset<span style="color: #009900;">&#40;</span><span style="color: #000088;">$property</span><span style="color: #009900;">&#41;</span>
  <span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span>property_exists<span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">,</span> <span style="color: #000088;">$property</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      throw <span style="color: #000000; font-weight: bold;">new</span> MagicMethodsException<span style="color: #009900;">&#40;</span>
        <span style="color: #0000ff;">&quot;Property '<span style="color: #006699; font-weight: bold;">$property</span>' not defined in class &quot;</span> <span style="color: #339933;">.</span> <span style="color: #990000;">get_class</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #009900;">&#41;</span>
      <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span>
      <span style="color: #000088;">$this</span><span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span><span style="color: #000088;">$property</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">null</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
  <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>


<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"> <span style="color: #339933;">*</span> <span style="color: #339933;">@</span>copyright <span style="color: #cc66cc;">2009</span><span style="color: #339933;">,</span> Juraj Seffer
 <span style="color: #339933;">*</span> <span style="color: #339933;">@</span>version <span style="color: #000088;">$Id</span>$
 <span style="color: #339933;">*</span> <span style="color: #339933;">@</span>access <span style="color: #000000; font-weight: bold;">public</span>
 <span style="color: #339933;">*/</span>
<span style="color: #000000; font-weight: bold;">class</span> MagicMethodsException <span style="color: #000000; font-weight: bold;">extends</span> Exception
<span style="color: #009900;">&#123;</span>
  <span style="color: #009933; font-style: italic;">/**
   * Constructor
   *
   * @param string $exception
   * @access public
   * @return void
   */</span>
   <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> __construct<span style="color: #009900;">&#40;</span><span style="color: #000088;">$exception</span><span style="color: #009900;">&#41;</span>
  <span style="color: #009900;">&#123;</span>
    throw <span style="color: #000000; font-weight: bold;">new</span> Exception<span style="color: #009900;">&#40;</span><span style="color: #000088;">$exception</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://jurajsplayground.com/2009/04/10/php-magic-methods-enforce-defining-of-class-properties/feed/</wfw:commentRss>
		</item>
	</channel>
</rss>

