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

NETTUTS.com: Can You Hack Your Own Site? A Look at Some Essential Security Considerations

Stefan Mischook's Blog: PHP Sessions Video Tutorials - Part 2

Ralph Schindler's Blog: Compiling Gearman (or anything) for Zend Server CE on Snow Leopard

Builder.au: Dynamic XML document construction with the PHP DOM

IBM developerWorks: PHP frameworks, Part 1: Getting started with three popular frameworks


Community Events

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


voicesoftheelephpant language example api opinion unittest podcast php7 xdebug framework introduction laravel version install community library extension series interview release

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