News Feed
Sections




News Archive
Looking for more information on how to do PHP the right way? Check out PHP: The Right Way

7PHP.com:
Auto Generate Properties Dynamically For Your Classes Using Magic Methods & Reflection
October 28, 2013 @ 12:57:14

Accessing private class properties via getters and setters is a pretty standard way to write your applications. Unfortunately it can be time consuming to write them for every property your class may have. On 7PHP.com Khayrattee Wasseem has a few ideas (including using PHP's own Reflection functionality) to dynamically create them.

When coding a project, at times (or most of it?) some classes might have more than 3 fields (for whatever reason it suits you). So instead of each time writing and repeating setters and getters (accessor methods), I would like to have a piece of reusable code for all my classes without me ever writing a single line of code for accessors. ('ever' as in 'very very rarely'). Now, we also have to take into consideration that some fields might be only get-able or only set-able - (our re-usable piece of code should cater for this)

He shows two different methods to accomplish this kind of dynamic access, one using traits and the other using normal class inheritance. HE includes the code illustration each solution and talks a bit at the end of each section of why that method might be better than the other.

0 comments voice your opinion now!
reflection getter setter private property tutorial trait inheritance

Link: http://7php.com/magic-dynamic-properties/

blog comments powered by Disqus

Similar Posts

Bhaskar Karambelkar\'s Blog: Prototyping LAMP with WAMP

VT\'s Tech Blog: A Few XML & PHP Tutorials

Giorgio Sironi's Blog: Practical PHP Patterns

Ben Ramsey's Blog: Build PHP 5.4 on CentOS 6.2

Ask About PHP: PHP and jQuery: Submit a form without refreshing the page


Community Events

Don't see your event here?
Let us know!


podcast api performance voicesoftheelephpant conference introduction configure language example part2 series symfony2 framework interview application opinion php7 release laravel community

All content copyright, 2015 PHPDeveloper.org :: info@phpdeveloper.org - Powered by the Solar PHP Framework