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

Zend Developer Zone: Search Engine Friendly Websites with the Zend Framework

Script-Tutorials.com: Form Validation with Javascript and PHP

Sebastian Bergmann's Blog: Testing Your Privates

Label Media Blog: Design Patterns in PHP - Decorator Pattern

Martin Sikora's Blog: Silex + Doctrine 2 ORM


Community Events





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


release community framework code language testing interview install series laravel list refactor experience podcast threedevsandamaybe developer introduction unittest symfony2 opinion

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