InfinityQuest - Programming Code Tutorials and Examples with Python, C++, Java, PHP, C#, JavaScript, Swift and more

Menu
  • Home
  • Sitemap

Python Programming Language Best Tutorials and Code Examples

Learn Python Right Now!
Home
PHP
Overriding Property Accesses in PHP
PHP

Overriding Property Accesses in PHP

InfinityCoder November 24, 2016

You want handler functions to execute whenever you read and write object properties. This lets you write generalized code to handle property access in your class.

Use the magic methods __get() and __set() to intercept property requests.

To improve this abstraction, also implement __isset() and __unset() methods to make the class behave correctly when you check a property using isset() or delete it using unset().

Property overloading allows you to seamlessly obscure from the user the actual location of your object’s properties and the data structure you use to store them.
For example, the Person class stores variables in an array, $__data. (The name of the variable doesn’t need begin with two underscores, that’s just to indicate to you that it’s used by a magic method.)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
class Person {
   private $__data = array();
 
   public function __get($property) {
      if (isset($this->__data[$property])) {
          return $this->__data[$property];
      } else {
          return false;
      }
}
 
public function __set($property, $value) {
   $this->__data[$property] = $value;
   }
}

Use it like this:

1
2
3
4
5
$johnwood = new Person;
$johnwood->email = 'jonathan@wopr.mil'; // sets $user->__data['email']
print $johnwood->email;                 // reads $user->__data['email']
 
jonathan@wopr.mil

When you set data, __set() rewrites the element inside of $__data. Likewise, use __get() to trap the call and return the correct array element.

Using these methods and an array as the alternate variable storage source makes it less painful to implement object encapsulation. Instead of writing a pair of accessor methods for every class property, you use __get() and __set().
With __get() and __set(), you can use what appear to be public properties, such as $johnwood->name, without violating encapsulation.

This is because the programmer isn’t reading from and writing to those properties directly, but is instead being routed through accessor methods.
The __get() method takes the property name as its single parameter. Within the method, you check to see whether that property has a value inside $__data. If it does, the method returns that value; otherwise, it returns false.

The __set() method takes two arguments: the property name and the new value. Otherwise, the logic inside the method is similar to __get().
Besides reducing the number of methods in your classes, these magical methods also make it easy to implement a centralized set of input and output validation.
Here’s how to also enforce exactly what properties are legal and illegal for a given class:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
class Person {
   // list person and email as valid properties
   protected $__data = array('person' => false, 'email' => false);
 
   public function __get($property) {
      if (isset($this->__data[$property])) {
          return $this->__data[$property];
      } else {
          return false;
      }
   }
 
   // enforce the restriction of only setting
   // predefined properties
   public function __set($property, $value) {
      if (isset($this->__data[$property])) {
          return $this->__data[$property] = $value;
      } else {
          return false;
      }
   }
}

In this updated version of the code, you explicitly list the object’s valid property names when you define the $__data property.

Then, inside __set(), you use isset() to confirm that all property writes are going to allowable names.
Preventing rogue reads and writes is why the visibility of the $__data property isn’t public, but protected. Otherwise, someone could do this:

1
2
$person = new Person;
$person->__data['fake_property'] = 'fake_data';

because the magical accessors aren’t used for existing properties.
Pay attention to this important implementation detail. In particular, if you’re expecting people to extend the class, they could introduce a property that conflicts with a property you’re expecting to handle using __get() and __set().

For that reason, the property in the earlier example is called $__data with two leading underscores.
You should consider prefixing all your “actual” properties in classes where you use magical accessors to prevent collisions between properties that should be handled using normal methods and ones that should be routed through __get() and __set().
There are three downsides to using __get() and __set(). First, these methods only catch missing properties. If you define a property for your class, __get() and __set() are not invoked by PHP when that property is accessed.
This is the case even if the property you’re trying to access isn’t visible in the current scope (for instance, when you’re reading a property that exists in the class but isn’t accessible to you, because it’s declared private).

Doing this causes PHP to emit a fatal error:

1
PHP Fatal error: Cannot access private property...

Second, these methods completely destroy any notion of property inheritance. If a parent object has a __get() method and you implement your own version of __get() in the child, your object won’t function correctly because the parent’s __get() method is never called.
You can work around this by calling parent::__get(), but it is something you need to explicitly manage instead of “getting for free” as part of OO design.
The illusion is incomplete because it doesn’t extend to the isset() and unset() methods.
For instance, if you try to check if an overloaded property isset(), you will not get an accurate answer, as PHP doesn’t know to invoke __get().
You can fix this by implementing your own version of these methods in the class, called __isset() and __unset():

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
class Person {
   // list person and email as valid properties
   protected $data = array('person' => false, 'email' => false);
 
   public function __get($property) {
      if (isset($this->data[$property])) {
          return $this->data[$property];
      } else {
          return null;
      }
   }
 
   // enforce the restriction of only setting
   // pre-defined properties
   public function __set($property, $value) {
      if (isset($this->data[$property])) {
          $this->data[$property] = $value;
      }
   }
   public function __isset($property) {
      return isset($this->data[$property]);
   }
 
   public function __unset($property) {
      if (isset($this->data[$property])) {
          unset($this->data[$property]);
      }
   }
}

The __isset() method checks inside the $data element and returns true or false depending on the status of the property you’re checking.
Likewise, __unset() passes back the value of unset() applied to the real property, or false if it’s not set.
Implementing these two methods isn’t required when using __get() and __set(), but it’s best to do so because it’s hard to predict how you may use object properties.

Failing to code these methods will lead to confusion when someone (perhaps even yourself) doesn’t know (or forgets) that this class is using magic accessor methods.
Other reasons to consider not using magical accessors are:
• They’re relatively slow. They’re both slower than direct property access and explicitly writing accessor methods for all your properties.
• They make it impossible for the Reflection classes and tools such as phpDocumentor to automatically document your code.
• You cannot use them with static properties.

Share
Tweet
Email
Prev Article
Next Article

Related Articles

Appending One Array to Another in PHP
You want to combine two arrays into one. Use array_merge(): …

Appending One Array to Another in PHP

Validating Form Input: Radio Buttons in PHP
You want to make sure a valid radio button is …

Validating Form Input: Radio Buttons in PHP

About The Author

InfinityCoder
InfinityCoder

Leave a Reply

Cancel reply

Recent Tutorials InfinityQuest

  • Adding New Features to bash Using Loadable Built-ins in bash
    Adding New Features to bash Using Loadable …
    June 27, 2017 0
  • Getting to the Bottom of Things in bash
    Getting to the Bottom of Things in …
    June 27, 2017 0

Recent Comments

  • fer on Turning a Dictionary into XML in Python
  • mahesh on Turning a Dictionary into XML in Python

Categories

  • Bash
  • PHP
  • Python
  • Uncategorized

InfinityQuest - Programming Code Tutorials and Examples with Python, C++, Java, PHP, C#, JavaScript, Swift and more

About Us

Start learning your desired programming language with InfinityQuest.com.

On our website you can access any tutorial that you want with video and code examples.

We are very happy and honored that InfinityQuest.com has been listed as a recommended learning website for students.

Popular Tags

binary data python CIDR convert string into datetime python create xml from dict python dictionary into xml python how to create xml with dict in Python how to write binary data in Python IP Address read binary data python tutorial string as date object python string to datetime python

Archives

  • June 2017
  • April 2017
  • February 2017
  • January 2017
  • December 2016
  • November 2016
Copyright © 2021 InfinityQuest - Programming Code Tutorials and Examples with Python, C++, Java, PHP, C#, JavaScript, Swift and more
Programming Tutorials | Sitemap